ismember on cell array and remplace values

6 visualizaciones (últimos 30 días)
sensation
sensation el 21 de Jun. de 2018
Editada: Stephen23 el 21 de Jun. de 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 comentarios
Adam
Adam el 21 de Jun. de 2018
Editada: Adam el 21 de Jun. de 2018
What is the error? For starters though:
c1(idx) = d2;
using parentheses is trying to change an element of a cell array and will expect a cell to be passed to it.
sensation
sensation el 21 de Jun. de 2018
error: Subscript indices must either be real positive integers or logicals. % probably because I have many repeated values in c1

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 21 de Jun. de 2018
Editada: Stephen23 el 21 de Jun. de 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 comentarios
sensation
sensation el 21 de Jun. de 2018
Thanks for your answer! why num2str conversion btw? Cheers
Stephen23
Stephen23 el 21 de Jun. de 2018
Editada: Stephen23 el 21 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by