Setting up Matrix in a Different way

2 visualizaciones (últimos 30 días)
A
A el 2 de Dic. de 2021
Comentada: A el 4 de Dic. de 2021
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 comentarios
Stephen23
Stephen23 el 2 de Dic. de 2021
Editada: Stephen23 el 2 de Dic. de 2021
@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 el 2 de Dic. de 2021
Editada: A el 4 de Dic. de 2021
@Stephen Sorry that was a mistake, I fixed it

Iniciar sesión para comentar.

Respuesta aceptada

the cyclist
the cyclist el 4 de Dic. de 2021
Editada: the cyclist el 4 de Dic. de 2021
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

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by