error-CAT arguments dimensions are not consistent.
Show older comments
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
Answers (1)
Walter Roberson
on 29 Jan 2012
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 Comments
kash
on 29 Jan 2012
kash
on 29 Jan 2012
Walter Roberson
on 29 Jan 2012
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
on 29 Jan 2012
Walter Roberson
on 29 Jan 2012
[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
on 29 Jan 2012
Walter Roberson
on 29 Jan 2012
You have an existing Question for that, http://www.mathworks.com/matlabcentral/answers/27353-selecting-top-values . It can be discussed there.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!