error-CAT arguments dimensions are not consistent.
이전 댓글 표시
i have a code
out = [d1, d(ismember(d(:,1),d1(:,1),'rows'),1:end)]
my values are
d=[1 2 3 ;5 6 8 ]
d1=[1 ;1]
i need the answer as
1 1 2 3
1 1 2 3
please help
답변 (1개)
Walter Roberson
2012년 1월 29일
d(:,1) is [1;5] . d1(:,1) is [1;1] . You ask ismember() for matching rows. The row [1] of d(:,1) matches the row [1] of d1(:,1) so the first value returned by ismember() is true. The row [5] of d(:,1) does not match any rows of dl(:,1) so the second value returned by ismember is false. The ismember result is thus [true;false] . You use that [true;false] as the first index of d, and you select all columns of d through the second index, so the result is going to be to select just the first row, d(:,1) which is [1 2 3]. You try to combine that single row via horzcat, with the multiple rows of d1, so you get an error.
The above is why you get an error. In order for us to show you how to get the output you want, you will need to explain the rules you want.
I note that if you were to use
[tf, ua, ub] = ismember(d1(:,1), d(:,1), 'rows'); %d1 and d change places
out = [d1(tf), d(ub,:)];
then that would happen to give the output you say you need. This is probably just a coincidence, however.
댓글 수: 7
kash
2012년 1월 29일
kash
2012년 1월 29일
Walter Roberson
2012년 1월 29일
I had a mistake in my sample code, and it is not easily repairable, so I won't try at this time.
What should be done if there are duplicate values in the first column of A? Should all matching columns be selected each time B matches that entry?
kash
2012년 1월 29일
Walter Roberson
2012년 1월 29일
[tf, loc] = ismember(B, A(:,1));
out = [reshape(B(tf),[],1), A(loc(tf), :)];
In the situation in which you are certain that every member of B occurs in A(:,1) this can be simplified to
[tf, loc] = ismember(B, A(:,1));
out = [reshape(B(loc),[],1), A(loc, :)];
kash
2012년 1월 29일
Walter Roberson
2012년 1월 29일
You have an existing Question for that, http://www.mathworks.com/matlabcentral/answers/27353-selecting-top-values . It can be discussed there.
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!