# Setting up Matrix in a Different way

1 vue (au cours des 30 derniers jours)
A le 2 Déc 2021
Commenté : A le 4 Déc 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 commentairesAfficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Stephen23 le 2 Déc 2021
Modifié(e) : Stephen23 le 2 Déc 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 le 2 Déc 2021
Modifié(e) : A le 4 Déc 2021
@Stephen Sorry that was a mistake, I fixed it

Connectez-vous pour commenter.

### Réponse acceptée

the cyclist le 4 Déc 2021
Modifié(e) : the cyclist le 4 Déc 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
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
A le 4 Déc 2021
Thank you so much!!!

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by