comparing table values using isequal

조회 수: 107 (최근 30일)
Jakub Steiner
Jakub Steiner 2020년 9월 15일
댓글: Cris LaPierre 2020년 9월 15일
Hi,
I have a table M. The second column contains either the letter 's' or letter 'f'.
I would like to put all the rows with the letter 'f' in the second column into a new table A with the following code
pocet_radku = height(M);
j=1;
for i=1:pocet_radku
TF = isequal ("M(i,2)",'f')
if TF == 1
A(j,:)= M(i,:);
j = j+1;
else
end
end
Unfortunatelly, the isequal function return only zeros. Any idea why? In there syntax error that I'm not aware of?
Testing in a command window:
>> isequal ("M(180,2)",'f')
ans =
logical
0
>> M(180,2)
ans =
table
Var2
_____
{'f'}
Thanks a lot for any answers

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 9월 15일
편집: Cris LaPierre 2020년 9월 15일
This can be done much simpler. Try something like this.
A = M(M{:,2}=="f",:);
  댓글 수: 2
Jakub Steiner
Jakub Steiner 2020년 9월 15일
Nice! Works as well and without my clumsy cycles.
Thanks a lot!
Cris LaPierre
Cris LaPierre 2020년 9월 15일
This can be made even simpler by using the variable name:
A = M(M.Var2=="f",:);

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2020년 9월 15일
편집: Fangjun Jiang 2020년 9월 15일
try TF = isequal (M.Var2{i},'f')

BOB MATHEW SYJI
BOB MATHEW SYJI 2020년 9월 15일
Instead of
TF = isequal ("M(i,2)",'f')
try,
TF = strcmp (M.2nd_variable,'f')

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by