Setting up Matrix in a Different way

2 ビュー (過去 30 日間)
A
A 2021 年 12 月 2 日
コメント済み: A 2021 年 12 月 4 日
I have a Matrices as:
A=[1 10 12;2 12 3; 3 15 4; 4 16 7; 5 18 9; 6 10 10; 7 12 9; 9 5 6];
B=[3 9 8; 4 8 3; 5 2 5; 7 10 2; 8 9 7; 9 12 10; 10 5 6];
Where Colum 1 is the index Colum 2 is the x and Colum 3 is the y. I would like to match each index of the 2 matrices and set the x and y values in Matirx C. Here Colum 1 is the A matrix values and Colum 2 is the B matrix values that is negative
The solution should be something like this:
C=[10 0;12 0; 12 0; 3 0; 15 -9; 4 -8;16 -8; 7 -3; 18 -2; 9 -5; 10 0; 10 0; 12 -10; 9 -2;0 -9; 0 -7;5 -12; 6 -10; 0 -5;0 -6]
How would I do this?
  4 件のコメント
Stephen23
Stephen23 2021 年 12 月 2 日
編集済み: Stephen23 2021 年 12 月 2 日
@AB: why does your example D matrix have the follow rows occuring only once:
0 -9
0 -7
even though the corresponding row occurs twice in B:
8 9 7
8 9 7
What is the rule for ignoring one of those rows of B ?
A
A 2021 年 12 月 2 日
編集済み: A 2021 年 12 月 4 日
@Stephen Sorry that was a mistake, I fixed it

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2021 年 12 月 4 日
編集済み: the cyclist 2021 年 12 月 4 日
This does what you want via a straightforward loop. I expect there is a slicker way to do this.
A=[1 10 12;
2 12 3;
3 15 4;
4 16 7;
5 18 9;
6 10 10;
7 12 9;
9 5 6];
B=[3 9 8;
4 8 3;
5 2 5;
7 10 2;
8 9 7;
9 12 10;
10 5 6];
AB1 = union(A(:,1),B(:,1));
C1 = [AB1';AB1'];
C = [C1(:),zeros(numel(C1),2)];
for nc = 1:size(C,1)
if any(A(:,1)==nc)
C([2*nc-1:2*nc],2) = A(A(:,1)==nc,2:3)';
end
if any(B(:,1)==nc)
C([2*nc-1:2*nc],3) = -B(B(:,1)==nc,2:3)';
end
end
C(:,1) = []
C = 20×2
10 0 12 0 12 0 3 0 15 -9 4 -8 16 -8 7 -3 18 -2 9 -5
  1 件のコメント
A
A 2021 年 12 月 4 日
Thank you so much!!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by