matching matrices sizes using if statements
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2013-8-14
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?
采纳的回答
kjetil87
2013-8-14
编辑:kjetil87
2013-8-14
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 个评论
kjetil87
2013-8-15
编辑:kjetil87
2013-8-15
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!