MATLAB Answers

assigning nested for loop value to matrix.

14 views (last 30 days)
ALDO
ALDO on 7 Oct 2019
Commented: ALDO on 11 Oct 2019
Hi guys i have a list of 41 names called N. I also have a cell called output.
'output' is a 375x1 cell. each cell is a table:
output:
4x3 table
3x3 table
196x3 table
1x3 table
100x3 table
....
each table has 3columns(Patient, number,Name)
Table1:
patient number Name
1 112 Name1
1 206 Name1
1 212 Name2
1 112 Name3
Table2:
patient number Name
2 11 Name15
2 20 Name10
2 33 Name2
....
I also have a list of refrence Names: N=Name1,Name2,Name3,....
I need to look at each cell and see if it has Name1. Then if it does look the colunm 'number' and find the minmun number. Then repeat the same porecess for the 41 Names per cell. so the result should be a 357X41 array. each column representing the names and each row representing each cell.
this is what i have so far but no matter how many versions i try to assign the values to an array or matrix. I cant seem to get it right! I would appriciate any help. lmk if there are any questions.
Thank you in advance!
b4=zeros(357,41);
for i=1:length(output) % for each cell
for j=1:length(N) %for each name
b=(output{i,1}.name==N(j)); %for i:here looking at table within each cell. then looking at column 'name'. for j:going overlist of names and calling each name
b1=(output{i,1}.number); %here looking at table within each cell. then looking at column 'number'
b2=(b1(b)); %finding columns in 'number' that are assosiated with the name i am looking for
b3=min(b2);%finding the minimun value of the column 'number' that matched the name i wanted
b4(i,j)=b3 %asign value to matrix
end

  0 Comments

Sign in to comment.

Accepted Answer

Bhargavi Maganuru
Bhargavi Maganuru on 11 Oct 2019
Line 4 in your code gives logical matrix and using it in Line6 gives error.
You can modify your code as
b4=zeros(357,41);
for i=1:length(output) % for each cell
for j=1:length(N) %for each name
b=(output{i,1}.name==N(j)); %for i:here looking at table within each cell. then looking at column 'name'. for j:going overlist of names and calling each name
if( nnz(b(:,5))~=0) %if N(j) is present,then find the minimum number corresponding to N(j)
b1=(output{i,1}.number); %here looking at table within each cell. then looking at column 'number'
b2=(b1(b(:,5)); %finding columns in 'number' that are assosiated with the name i am looking for
b3=min(b2);%finding the minimun value of the column 'number' that matched the name i wanted
b4(i,j)=b3 ;%asign value to matrix
end
end
end
Hope this helps!

  1 Comment

ALDO
ALDO on 11 Oct 2019
Thank you so much for taking the time to look over this! This helped me immensely:)

Sign in to comment.

More Answers (0)

Sign in to answer this question.