How to create a variable where certain subjects are coded as 1?

Hi all.
A Matlab beginner here, would appreciate some advice.
I want to create a variable called 'flag_artifact' where certain subjects from my dataset (for whom I know have bad quality images) are coded as e.g., 1. My dataset is stored in a table T with a certain number of rows and 'subject' is the 1st column in the table.
I managed to do it by creating a for loop. Here is what I have:
flag_artifact = {'T_300'};
for i = 1:size(T,1)
if isequal(table2cell(T1(i, 1)), flag_artifact)
T(i, 1) = {'1'};
end
end
Surely there is a more efficient way to do this by perhaps directly creating the variable and using less lines of code? Could anyone give some advice?
Thank you very much!

 Akzeptierte Antwort

Matt J
Matt J am 2 Mai 2022
Bearbeitet: Matt J am 2 Mai 2022
flag_artifact=strcmp(T.subject,'T_300');
T.subject(flag_artifact)='1';

6 Kommentare

Hi, @Matt J - thank you for your answer.
When I try the above, I get an error message ''Conversion to cell from char is not possible.''.
Is there a way to specify multiple subjects in this variable 'flag_artifact' that I would like to code as 1?
Maybe,
T.subject(flag_artifact)={'1'};
Thank you for this. The line you suggest does work, but what I am unable to do is to specify multiple subjects when defining the variable flag_artifact. For example:
flag_artifact = {'T_300'}; %is there a way to specify multiple subjects here?
I tried using
flag_artifact = {'T_300'}; {'T_301'}
flag_artifact = {'T_300', 'T_301'}
However, it doesn't work because these subject identifiers do not get replaced with 1s. So the loop always performs the operation for the first defined subject, not for multiple and this is what I am struggling with.
flagged=ismember(T.subject,{'T_300', 'T_301'});
T.subject(flagged)={'1'};
Hi Matt J.
This is great, thank you! :)
You are quite welcome, but please Accept-click the answer to certify that it worked.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by