Convert cell array with characters to vector with numbers?

Hi guys,
I've been trying to get a cell array that looks like this:
y =
'a'
'a'
'a'
'b'
'b'
'a'
'a'
'b'
'a'
'b'
'b'
'b'
to convert it to a vector that looks like this: y =
1
1
1
0
0
1
1
0
1
0
0
0
How do I do this? I've been trying to get it right with strfind and using it on the cell array and use those results to alter the contents of the cell array before I convert it to a vector with cell2mat().
But no luck so far.
Can someone help me with this?
Thanks in advance.

 채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 17일
ismember(y, 'a')

댓글 수: 1

If your array "y" is large, or you do this operation many times, you might want to do some timing experiments. On my machine, ismember() takes about 10 times longer than strcmp() to do this operation on moderately large arrays.

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

추가 답변 (2개)

the cyclist
the cyclist 2012년 6월 17일
One of many ways:
strcmp(y,'a')

댓글 수: 1

I prefer this, because it is lean and fast for small and large input.

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

카테고리

도움말 센터File 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