mHelpMe
mHelpMe

Reputation: 6668

using ismember for numeric values

I have a 4554 x 1 vector of type double, called company_info,ind_vec. I also have a another 25 x 1 vector which is of type cell array called groups.industy_labels.

groups.industy_labels contains a list of codes which are numeric. company_info,ind_vec contains the same numeric codes.

I was planning on doing the below, where I use ismember to return the indices for each numeric code in groups.industy_labels and then do a sum on another vector which is realted to company_info,ind_vec, i.e. another 4554 x 1 vector.

[~, index_sub]              = ismember(company_info.ind_vec, groups.industy_labels);
groups.industy_exps(:, 1)   = accumarray(index_sub, pwgt , [], @sum, 0);

However Matlab is telling me that ismember only takes cell arrays of string. Is there another way of doing this?

Upvotes: 0

Views: 768

Answers (1)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Actually the error message is a bit deceptive as you can use ismember for numeric values:

x=[1 3]
y=[1 2]   
ismember(x,y) %This will work

You can also use it for cell arrays, but only for strings:

x=[{'a'},{'c'}]
y=[{'a'},{'b'}]
ismember(x,y) %This will work

x=[{1},{3}]
y=[{1},{2}]
ismember(x,y) %This will fail

So in your case, you would want to use it on 2 numeric vectors, rather than 1 numeric vector and 1 cell array:

x=[1,2] %Numeric vector
y=[{1},{2}] %Cell array

y_numeric = [y{:}] %Made into a numeric vector

ismember(x,y_numeric) %This will work

Note that this assumes that each entry in the cell array only contains a number.

Upvotes: 1

Related Questions