Setting up Matrix in a Different way
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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?
Respuesta aceptada
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) = []
Más respuestas (0)
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices 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!