ismember cell array of string and double vector

조회 수: 11 (최근 30일)
gianluca mattei
gianluca mattei 2016년 6월 10일
답변: gianluca mattei 2016년 6월 10일
Hi all, I have one cell array
list_1={'34172','36582','482','52792'}
and a double vector
vector_1=[21312,782,482,132]
how to know which element of cell array is present in vector_1 and viceversa?
  댓글 수: 2
Guillaume
Guillaume 2016년 6월 10일
편집: Guillaume 2016년 6월 10일
Is vector_1 equal to ['21312','782','482','132'] (which is simply the string '21312782482132', or equal to [21312, 782, 282, 132] (with no quotes), a vector of numbers?
The two are very different.
And is vector_1 a cell array (as per the title of your question) or a matrix (as per your example)?
gianluca mattei
gianluca mattei 2016년 6월 10일
I have edited the question: vector_1 is a simple double vector meanwhile list_1 is a cell array

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 6월 10일
편집: Guillaume 2016년 6월 10일
See my comments to your question, the example you provide does not match the title of your question. Assuming:
list_1 = {'34172', '36582', '482', '52792'};
vector_1 = [21312, 782, 282, 132];
You need to convert the type of one to the type of the other in order to compare them. So either convert the cell array of strings into a vector of number
list_1_as_numbers = str2double(list_1);
ismember(list1_as_numbers, vector_1)
or convert the numbers to string:
vector_1_as_string = sprintfc('%d', vector_1); %warning: sprintfc is not officially supported
%officially supported version is:
%vector_1_as_string = arrayfun(@(n) sprintf('%d', n), vector_1, 'UniformOutput', false);
ismember(list1, vector1_as_string)

추가 답변 (1개)

gianluca mattei
gianluca mattei 2016년 6월 10일
str2double works, stupid question indeed. I have tried everything found on forum but the solution was quite easy.
thanks

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by