Hello, I have a question that,
if A=[ 'ddd', 'aaa']; B=['ddd']; I want to return an array that compare whether A's cell is equivalent to B's content. (sorry I know it is not well written)
So it should be [1,0] where first cell is equivalent but second is not.
However, my code has a problem:
for j=1:Num
temp(j)=(A{j}==B{1});
end
that it returns a comparison of vector, for example temp(1)=[1 1 1]; How can I do a comparison and just return 1 or 0, not an array.
How can I do it vectorization?
Thanks.

 채택된 답변

Star Strider
Star Strider 2015년 4월 14일

1 개 추천

To cast your array as cells, use curly brackets ‘{}’ rather than square brackets ‘[]’.
With that change:
A={'ddd', 'aaa'};
B={'ddd'};
temp = strcmpi(A,B)
produces:
temp =
1 0
as desired.

댓글 수: 8

C Zeng
C Zeng 2015년 4월 14일
Thanks, however, it seems not able to do a vector check, like strcmpi(A,B) that A is a vector of contents where B is just a cell.
Star Strider
Star Strider 2015년 4월 14일
That is because MATLAB interprets two strings in a matrix or vector as a concatenation.
So ['ddd', 'aaa'] will be intrepreted as ['dddaaa']. Using cells instead of matrices works the way you intend in your code.
C Zeng
C Zeng 2015년 4월 14일
I see, I read from Excel file, so it is a long column with each cell as the entry.
I will use for loop then. Thanks.
Star Strider
Star Strider 2015년 4월 14일
My pleasure.
A cell array as provided by xlsread should work. That would likely be the the second output provided by xlsread. (The first output would be a numeric array.)
C Zeng
C Zeng 2015년 4월 14일
yes, [num,txt,raw]=xlsread(filename)
So I should use the second one? However, I see txt only read non-numeric value, but I want both. P.S. in my excel file, it has both numeric and non-numeric values. so I guess I need raw?
It depends on what you are doing. To compare strings in a cell array, use the ‘txt’ output. If you are interested in accessing the numeric output in ‘num’ corresponding to a particular ‘txt’ entry, you can add that as a second assignment in your for loop.
For example:
A = {'ddd'; 'aaa'};
B = {'ddd'};
num = [1 2; 3 4];
Num = size(A,1);
for j=1:Num
temp{j} = strcmpi(A{j},B{1});
if temp{j} ~= 0
data{j} = num(j,:);
end
end
temp{:} % Display Results
data{:} % Display Results
C Zeng
C Zeng 2015년 4월 15일
Thanks Star!
Star Strider
Star Strider 2015년 4월 15일
My pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

질문:

2015년 4월 14일

댓글:

2015년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by