Error too many input arguments using equals equals

1 vue (au cours des 30 derniers jours)
Sophia
Sophia le 16 Mai 2019
Réponse apportée : Voss le 13 Fév 2024
Each row of the table want contains information about a specific person. Each row of the cell array (I tried having it be a table and got different errors) roster has information about a specific person and their subject ID. I want to get the subject ID of every person in the table want. To do this, I am saying (I think I'm saying) for every row in want to find the row in roster that has the same value in column 3 as want's column 3 and the same value in roster's column 2 as want's column 2. Then the array PTID will print all of the subject IDs that I want.
Errors:
-line 5: too many input arguments for == (it does this with and without the and)
-line 5: I think the {:,3} and {:,2} might not necissarily mean the same row in both cases
-line 1 & 2 (currently not an error, but might cause other errors): having want be a table and roster be a cell doesn't allow it to do the same things as matrices does, but I am unable to turn them into matrices.
-previously I had the error == was an undefined operator for cell type, but fixed it by using curly brackets
1 want=readtable('miniStable_MCI.csv');%the file to copy into matrix
2 roster = table2cell(readtable('miniROSTER.csv'));
3 PTID=zeros(4439);
4 for j = 1:4439
5 rosterrow=find((roster{:,3}==want{j,3})&(roster{:,2}==want{j,2}));%row 3
6 ...has RID, row 2 has site ID, and both values must be the same as the
7 ...2 values in the other matrix
8 PTID(j)=roster(rosterrow,5);%roster has subject ID in column 5
9 end
sprintf(PTID)
example rows of want:
[ADNI1 39 267 2035; ADNI1 5 267 50; ADNI2 39 19 50]
example row of roster:
[ADNI1 39 82 50 149_S_0082; ADNI1 14 19 002_S_0019; ADNI1 5 267 50 014_S_0267]
So row 2 of want would correlate with row 3 of roster and output subject ID 014_S_0267]
Thank you so much!!

Réponses (1)

Voss
Voss le 13 Fév 2024
"I tried having it be a table and got different errors"
Let me keep both want and roster as tables.
want = table({'ADNI1';'ADNI1';'ADNI3'},[39; 5; 39],[267; 267; 19],[2035; 50; 50])
want = 3×4 table
Var1 Var2 Var3 Var4 _________ ____ ____ ____ {'ADNI1'} 39 267 2035 {'ADNI1'} 5 267 50 {'ADNI3'} 39 19 50
roster = table({'ADNI1';'ADNI1';'ADNI1'},[39;14;5],[82;NaN;267],[50;19;50],{'149_S_0082';'002_S_0019';'014_S_0267'})
roster = 3×5 table
Var1 Var2 Var3 Var4 Var5 _________ ____ ____ ____ ______________ {'ADNI1'} 39 82 50 {'149_S_0082'} {'ADNI1'} 14 NaN 19 {'002_S_0019'} {'ADNI1'} 5 267 50 {'014_S_0267'}
for-loop solution:
N = size(want,1);
PTID = cell(N,1);
PTID(:) = {'<NO MATCH>'};
for jj = 1:N
rosterrow = find((roster{:,3}==want{jj,3})&(roster{:,2}==want{jj,2}), 1);
if ~isempty(rosterrow)
PTID(jj) = roster{rosterrow,5};
end
end
disp(PTID)
{'<NO MATCH>'} {'014_S_0267'} {'<NO MATCH>'}
vectorized solution:
N = size(want,1);
PTID = cell(N,1);
PTID(:) = {'<NO MATCH>'};
[ism,idx] = ismember(want{:,[2 3]},roster{:,[2 3]},'rows');
PTID(ism) = roster{idx(ism),5};
disp(PTID)
{'<NO MATCH>'} {'014_S_0267'} {'<NO MATCH>'}

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by