Using a condition for two string comparison (strcmp)

22 ビュー (過去 30 日間)
Ana Carolina  Vallone Orlandin
Ana Carolina Vallone Orlandin 2022 年 4 月 14 日
コメント済み: Voss 2022 年 4 月 14 日
Hello!
This is my first week using matlab. I was given an assignment in which I have to use strcmp to compare two different columns for two matching elements (for example, if one contains A, the other has to contain A as well, then I know I get a logical 1 in return.). However, I also need a third column to be true, while this logical 1 is bein computed. So I need for when columns "stimulus" and "response" have the same element, that this is only going to be computed as a match if the word 'Sequence' is present in column "condition". I have tried to add for, if and finally while loop... But none work and none retrieve me a logical. My last attempt was this the statement exist that i found online:
S1 = myTable.stimulus
S2 = myTable.response
while exist('myTable.condition', 'var') == 'Sequence'
end
isCorrect = strcmp(S1, S2)
However, it does not work...
I asked the teacher and he keeps challenging me to google for finding the answer. I have not found anything that helps. The problem is that I am not familiar with many commands yet (he only exaplained how to use these statemnets for running loops, which I believe I don't need in this case).
I would appreciate any ideas on how to use strcmp with a condition when a third variable plays an important role over the data selection.

回答 (1 件)

Voss
Voss 2022 年 4 月 14 日
編集済み: Voss 2022 年 4 月 14 日
stimulus = {'A'; 'C'; 'A'; 'B'};
response = {'A'; 'C'; 'B'; 'A'};
condition = {'not Sequence'; 'Sequence'; 'Sequence'; 'not Sequence'};
t = table(stimulus,response,condition)
t = 4×3 table
stimulus response condition ________ ________ ________________ {'A'} {'A'} {'not Sequence'} {'C'} {'C'} {'Sequence' } {'A'} {'B'} {'Sequence' } {'B'} {'A'} {'not Sequence'}
isCorrect = strcmp(t.stimulus,t.response) & strcmp(t.condition,'Sequence')
isCorrect = 4×1 logical array
0 1 0 0
  2 件のコメント
Ana Carolina  Vallone Orlandin
Ana Carolina Vallone Orlandin 2022 年 4 月 14 日
Thank you! I thought that the & command had to be used with two && when i tried that :D
Voss
Voss 2022 年 4 月 14 日
You're welcome!
&& is for scalars only. Since the code here is dealing with vectors (i.e., each column of the table is a vector with multiple elements), we have to use &, and the result is also a vector.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by