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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by