Scanning a column in matrix with repeated values

There are four coulmns (Subject, Number, ID, ANS) in a table named 'List'. I initialized a structure with each cell having zero matrices for unique value in subject list based on below code. Once this is done I want to scan the column Subject(which has repeated values) and place the ANS value of each row in the Zero matrix of that subject number at 'IDxNumber' (index). This repeates for every subject value found in the Subject column which is 10 next.
List table
Subject Number ID ANS
9 1 10 -1
9 2 10 1
9 3 10 -1
10 1 5 1
10 2 6 -1
c = 1;
for b = 1:max(List.Subject)
for a = 1:length(List.Subject)
if List.Subject(a) == b
count_id = sum(List.Subject(:) == b);
Subject_Assist{c} = zeros(101,count_id);
c = c+1;
end
end
end

2 comentarios

the cyclist
the cyclist el 19 de Dic. de 2018
Editada: the cyclist el 19 de Dic. de 2018
What MATLAB data type is List? Is it really a table? Or is it a cell array or some other data type? The reason I ask is that sometimes people use the term "table" a little more loosely.
I've added a solution below under the assumption that it is really a table.
Sunny
Sunny el 19 de Dic. de 2018
Its a table.

Iniciar sesión para comentar.

 Respuesta aceptada

the cyclist
the cyclist el 19 de Dic. de 2018
Editada: the cyclist el 19 de Dic. de 2018
This code does the initialization. I'm still trying to figure out what you mean about filling it in.
[Edited afterward to fill in.]
% The original data
Subject = [9;9;9;10;10];
Number = [1;2;3;1;2];
ID = [10;10;10;5;6];
ANS = [-1;1;-1;1;-1];
% The original table
List = table(Subject,Number,ID,ANS);
% Find the unique subjects, and their corresponding rows
[uniqueSubject,~,indexFromUniqueBackToAll] = unique(List.Subject);
numberUniqueSubjects = numel(uniqueSubject);
% Create a cell array with one element per subject
output = cell(numberUniqueSubjects,1);
% For each subject ...
for ns = 1:numberUniqueSubjects
% Index that subject;s rows
indexThisSubjectsRows = find(indexFromUniqueBackToAll==ns);
numberRowsThisSubject = numel(indexThisSubjectsRows);
% initialize a matrix of zeros of apprpriate size
output{ns} = zeros(101,numberRowsThisSubject);
% Fill in ANS at the indicated rows
for ni = 1:numberRowsThisSubject
output{ns}(List.ID(indexThisSubjectsRows(ni)),List.Number(indexThisSubjectsRows(ni))) = List.ANS(indexThisSubjectsRows(ni));
end
end

5 comentarios

Sunny
Sunny el 19 de Dic. de 2018
Thanks, after initializing the zero matrices for each unique values. The next step I am trying to do is scan through 'Subject' column again and for each row I need to pick the ANS value and place it in the zero matrix relevant to that Subject number in a cell whose index is known from ID x Number.
For example:
From the table in question. for first row. The value -1 from ANS columnshould be placed in the zero matrix of subject 9 at 10th row 1st column. Then it goes to second row and picks ANS value 1 from ANS and places in 10th row second column.Once the subject changes then it should access relevant zero matrix of that subject and place values accordingly. The data I provided is a sample.
Thanks
the cyclist
the cyclist el 19 de Dic. de 2018
Ah, I understand. I've modified the above code to do that.
I expect there are some more efficient ways to do it, but this way is straightforward.
Sunny
Sunny el 19 de Dic. de 2018
Editada: Sunny el 19 de Dic. de 2018
Thanks it works, figuring out one error on the dataset. "Index in position 1 is invalid. Array indices must be positive integers or logical values." for this step.
output{ns}(List.ID(indexThisSubjectsRows(ni)),List.Number(indexThisSubjectsRows(ni))) = List.ANS(indexThisSubjectsRows(ni));
the cyclist
the cyclist el 19 de Dic. de 2018
If you want to upload a MAT file with your full table List, I can try to take a look.
Sunny
Sunny el 20 de Dic. de 2018
I figured out the issue its due to NaN. Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2018a

Etiquetas

Preguntada:

el 19 de Dic. de 2018

Comentada:

el 20 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by