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!

 Accepted Answer

RARmat = zeros(65, 3)
for ii = 1:65
RARatio = AUC/RA;
disp(RARatio)
% RAR Matrix
RARmat(ii, :) = RARatio;
end

11 Comments

Jo E.
Jo E. on 22 Sep 2019
Edited: Jo E. on 22 Sep 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?
Not quite working. But thank you anyway!
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.
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.
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.
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. on 22 Sep 2019
Edited: Jo E. on 22 Sep 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.
You would be better off starting a new question (also link this thread to that question), so that you get a help soon.
Will do. Thank you for your help.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 22 Sep 2019

Edited:

on 22 Sep 2019

Community Treasure Hunt

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

Start Hunting!