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 Comment

i get error wen my code is
out = [d1, d(ismember(d(:,1),d1(:,1),'rows'),1:end)]
error is
CAT arguments dimensions are not consistent
please help

Sign in to comment.

Answers (1)

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

walter i get error
Error using ==> ismember
Too many output arguments.
i need output as
id my A matrix=
[1 2 3
5 6 3
9 5 8 ]
B=[1;5;5]
i want to compare first column of A and B,and i want to select the corresponding A matrix with B,so my output must be as
1 1 2 3
5 5 6 3
5 5 6 3
i have large matrix of 100x42 for A
and 100X1 for B
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?
there are no duplicate values in A,only in B tere will b,i value of B matches with A ,that column must be displayed
[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, :)];
Thanks walter,
in other code
ind=nchoosek(1:100,2);
data12=[1:100]
FC1=data12(ind)
my FC1 has 2 column of 4900 rows,but i need 2 columns and 50 rows,by seleccting some values ,please help
for example selecting 2 values from each 100 ,(wen selecting column values must not change)
You have an existing Question for that, http://www.mathworks.com/matlabcentral/answers/27353-selecting-top-values . It can be discussed there.

Sign in to comment.

Categories

Tags

Asked:

on 29 Jan 2012

Community Treasure Hunt

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

Start Hunting!