필터 지우기
필터 지우기

how do I compare columns in a string matrix

조회 수: 1 (최근 30일)
ilona
ilona 2013년 11월 23일
댓글: ilona 2013년 11월 23일
I have a given string matrix and I need to check if the word can be read the same from the opposite direction (that it is to say if it is a Palindromes like -live not on evil)
%
wordList = ['civic';'dream';'kayak';'level';...
'lower';'peace';'radar';'refer';'table';'stats']
if the word is read the same from both direction I have to put 1 in the vector palRes(10,1) if not - I have to put 0 in it...
what I have done:
%
wordList = ['civic';'dream';'kayak';'level';...
'lower';'peace';'radar';'refer';'table';'stats']
wordList1 = fliplr(wordList)
palRes=zeros(10,1)
for ii= 1:size(wordList,1)
for jj = 1:size(wordList,2)
if wordList(ii)== wordList1(ii)
palRes(ii,jj) = 1
end
end
end
but the problem is that the final palRes that I get is a(10,5)matrix instead of (10,1) vec I want to fix this problem inside the loop and not after it! any ideas? thanks!

채택된 답변

Roger Stafford
Roger Stafford 2013년 11월 23일
Instead of that inner for-loop with the index jj, use the 'all' function:
palRes(ii) = all(wordList(ii)==fliplr(wordList(ii));
  댓글 수: 3
Roger Stafford
Roger Stafford 2013년 11월 23일
You can read about it in detail at:
http://www.mathworks.com/help/matlab/ref/all.html
In your case it is true if each logical element in its vector argument is true.
I have assumed here that wordList(ii) is a vector of string characters. Perhaps it should be written as wordList(ii,:).
ilona
ilona 2013년 11월 23일
I did as you told but still got it wrong
what I did:
% code
wordList = ['civic';'dream';'kayak';'level';...
'lower';'peace';'radar';'refer';'table';'stats']
wordList1 = fliplr(wordList)
palRes=zeros(10,1)
for ii= 1:size(wordList,1)
if all(wordList(ii)==fliplr(wordList(ii)));
palRes(ii) = 1
end
end
doing this
palRes =
1
1
1
1
1
1
1
1
1
1
which is a wrong answer!!!
what have I done wrong :( ?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by