Store values from a for loop into a matrix

Hello all, if anyone could possibly help me I would greatly appreciate it. I am attempting to store values calculated in a for loop in a matrix. I have already tried to preallocate arrays and create an empty matrix but just cant seem to get it to work.
The matrix would be [65x3] because there are 3 conditions and 65 participants. The for loop calculates one condition from one participant at a time.
for RARs = zeros(65, 3)
i = 1:65;
RARatio = AUC/RA;
disp(RARatio)
% RAR Matrix
RARmat(RARs, :) = RARatio;
end
Thank you in advance!

 Respuesta aceptada

madhan ravi
madhan ravi el 22 de Sept. de 2019
RARmat = zeros(65, 3)
for ii = 1:65
RARatio = AUC/RA;
disp(RARatio)
% RAR Matrix
RARmat(ii, :) = RARatio;
end

11 comentarios

Jo E.
Jo E. el 22 de Sept. de 2019
Editada: Jo E. el 22 de Sept. de 2019
Thank you so much!! That definitely works!
Follow up question: Is there any way to qualify that every third iteration of the for loop starts a new row?
ii = 3:3:65
Jo E.
Jo E. el 22 de Sept. de 2019
Not quite working. But thank you anyway!
madhan ravi
madhan ravi el 22 de Sept. de 2019
What’s not working ? It’s always a good idea to illustrate of what you’re describing, so that it’s easy for people to answer.
Jo E. comments:
I attached a picture of what is coming up when I added this code
ii = 3:3:65
What I am actually looking to do is that each participant has 1 row of 3 columns meaning that 3 itirations of the loop will fill the first row across 3 colums and the 4th itiration will start a new row.
Thank you for the help.
madhan ravi
madhan ravi el 22 de Sept. de 2019
Editada: madhan ravi el 22 de Sept. de 2019
Please don't keep adding answers just to make a comment. Use Comment on this Answer instead.
Perhaps you just want:
ii = 1:3:65
Even if the above doesn't do what you want , you need to show how your expected result should look like with an explicit example.
Jo E.
Jo E. el 22 de Sept. de 2019
I have attached a photo of what I am looking to be populated in the matrix. After 6 itirations of the loop I will need 2 rows of 3 conditions. I hope that clarifies what I am asking.
madhan ravi
madhan ravi el 22 de Sept. de 2019
Editada: madhan ravi el 22 de Sept. de 2019
You keep on changing your question in each comment. Your original question was how to store values in each iteration. Attach your data as .mat file.
Jo E.
Jo E. el 22 de Sept. de 2019
Editada: Jo E. el 22 de Sept. de 2019
Yes and you answered it wonderfully. I asked the follow up question of how to store values of each itiration in a new row after the 3rd iteration. Thank you for your help thus far.
madhan ravi
madhan ravi el 22 de Sept. de 2019
You would be better off starting a new question (also link this thread to that question), so that you get a help soon.
Jo E.
Jo E. el 22 de Sept. de 2019
Will do. Thank you for your help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Sept. de 2019

Editada:

el 22 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by