ismember on cell array and remplace values
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
sensation
am 21 Jun. 2018
Bearbeitet: Stephen23
am 21 Jun. 2018
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.
2 Kommentare
Akzeptierte Antwort
Stephen23
am 21 Jun. 2018
Bearbeitet: Stephen23
am 21 Jun. 2018
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?
6 Kommentare
Stephen23
am 21 Jun. 2018
Bearbeitet: Stephen23
am 21 Jun. 2018
"why num2str conversion btw?"
Because your first comment clearly showed that you wanted the output to be char vectors in a cell array. So I gave you char vectors in a cell array. Then in a later comment you apparently changed your mind and showed that you wanted scalar numeric in a cell array, and so I removed the num2str conversion.
I cannot read your mind, so I have to follow what you show/explain in your comments, which I tried to do as best I can.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!