matching matrices sizes using if statements

Good Afternoon,
I was wondering if someone could assist me on an if condition. I have two data sets A [nx3] and B[mx3]. I would like the rows of each data set to match one another with repeat values of that data set. For example if
A=[ 1 2 3;
4 5 6;
7 8 9]
and
B=[4 5 8;
1 2 7].
Then B would become
[4 5 6;
1 2 7;
4 5 6];
Something like
[ar,ac]=size(A);
[br,bc]=size(B);
if ar = br
A;
B;
if ar > br
if ar < br
Any suggestions?
Thanks!!
[Melissa, I took a little free rein to edit your code and make the question a little clearer. -- the cyclist]

4 commentaires

the cyclist
the cyclist le 14 Août 2013
I don't understand how the new B is derived from A and the old B.
Melissa
Melissa le 14 Août 2013
New_B has to be the same size as A. So New_B will contain multiple values of Old_B until it reaches the same size as A. Sorry for being unclear.
Still me no follow...how did you get the particular rows in new_B again? Need a definitive prescription to be able to write any code...otherwise why isn't
B=[4 5 8;
1 2 7;
1 2 7]
just as valid? In fact, other than size(A,1), what does A have to do with it at all by your description?
The B mentioned above would be valid. I just need the number of rows to match both in A and B. If A has more rows than B then New_B will just repeat B's rows until it matches the row size of A. It doesn't matter what rows of B that are used to get the matrix dimensions to match A.
Example: A=[1 2 3; 4 5 6; 7 8 9]; B=[4 5 6; 7 8 9];
New_B can equal
[4 5 6; 7 8 9; 4 5 6] or [4 5 6; 7 8 9; 7 8 9]

Connectez-vous pour commenter.

 Réponse acceptée

kjetil87
kjetil87 le 14 Août 2013
Modifié(e) : kjetil87 le 14 Août 2013
A=[ 1 2 3;...
4 5 6;...
7 8 9];
B=[4 5 8;...
1 2 7];
[ma,na]=size(A);
[mb,nb]=size(B);
if ma>mb
N=ceil(ma/mb);
B=repmat(B,N,1);
B=B(1:ma,:)
elseif mb>ma
N=ceil(mb/ma);
A=repmat(A,N,1);
A=A(1:mb,:)
end
%else they have equal number of rows, no operation needed.

5 commentaires

Thanks for the answer kjetil87. That worked amazing but when I implemented it in my code A added an exta row. Any idea why?
Orig_A=A;
Orig_B=B;
%Finding Centroids of Data Sets
Centroid_A = mean(A);
Centroid_B = mean(B);
%Matching Matrix Dimensions
[ma,na]=size(A);
[mb,nb]=size(B);
if ma>mb
N=ceil(ma/mb);
New_B=repmat(B,N,1);
New_B=New_B(1:ma,:);
New_A=A;
elseif mb>ma
N=ceil(mb/ma);
New_A=repmat(A,N,1);
New_A=New_A(1:mb,:);
New_B=B;
else A=B;
A=A;
B=B;
end
A
B
New_A
New_B
Hmm... when did this happen? was it A the largest or B the largest? Could you give two example matrices that produces the unexpected extra row?
Also this last test of yours is probably not what you intended, if the sizes ma and mb is equal your code will assign B to A.
example:
A=[1,2];
B=[2,3];
if false
%do nothing, never happens
else A=B; %here you set A equal to B
end
As you can see from A after running this code it now holds the values in B, because you assigned after your else. I recommend you either remove the entire else as it is not needed (your are not doing anything) or remove the A=B;
Or when you say added an extra row, do you perhaps mean that it added an extra column? =)
If so it is probably related to the above mentioned else statement. I think what you intended to write was:
elseif ma==mb
A=A;
B=B;
end
to clarify even more, these two statements are the exact same:
if
....
else A=B;
A=A;
B=B;
end
and second:
if
....
else
A=B;
A=A;
B=B;
end
So if a grew with an extra column it was probably because A and B looked something like this:
A=[1,2;...
4,5];
B=[1,2,3;...
4,5,6];
You are right I meant if ma=mb for the last else statement. Thank you for correcting me.
Try A=[1 2 3; 4 5 6; 7 8 9]; B=[3 6 8; 9 1 4];
Wait...Once I fixed that part it worked. Huh. Thanks!!
kjetil87
kjetil87 le 15 Août 2013
Modifié(e) : kjetil87 le 15 Août 2013
Glad to help, but again you are not using the "=" correct.
= will assign right hand side to left hand side.
== will compare left and right side and return logical 1 if they are equal, false if they are not and an error if they are not comparable (say e.g different sizes)
if you confuse them in an if test matlab will return you and error , but as you saw from your earlier code its important to use them correctly.
Glad the code worked. If the two matrices above caused problems, it might be because you ran the code several times and A and B did not have the content you thought when you started.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by