MATLAB: Ismember on cell array and remplace values

indexismemberremplace

Hi, I am trying to use ismember and replace values but getting an error. Any hint? Thanks
c1={'ESP00021';'ESP00023';'ESP00023';'ESP00025'};
c2={'ESP00023';'ESP00025'};
d2=[5;26];
d2=num2cell(d2);
[~,idx] = ismember(c2,c1,'rows');
c1(idx) = d2;
error: Subscript indices must either be real positive integers or logicals. % probably because I have many repeated values in c1.

Best Answer

c1={'ESP00021','ESP00023','ESP00023'}; % a row vector!

c2={'ESP00023','ESP00025'}; % a row vector!
[~,idx] = ismember(c2,c1,'rows');
Question: how many matching rows do you have?
Answer: none.
Both of the cell arrays constitute exactly one row. Those two rows are different (both the contents and number of elements), so there are no matching rows and the output idx would not be a valid index... if it worked at all, which I doubt because ismember is not specified to work with cell array input and the 'rows' option. Neither does it work when I tried it with numeric arrays with a different number of columns:
>> [~,idx] = ismember([1,2,3],[45,45],'rows')
Error using ismember>ismemberlegacy (line 276)
Inputs A and B must be matrices with the same number of columns in the 'rows' case.
The cannot be fixed by simple removing 'rows' because the second char vector in c2, 'ESP00025', does not match any char vector in c1 so the second index value will still be zero (not a valid index).
What are you actually trying to achieve?