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!
采纳的回答
RARmat = zeros(65, 3)
for ii = 1:65
RARatio = AUC/RA;
disp(RARatio)
% RAR Matrix
RARmat(ii, :) = RARatio;
end
11 个评论
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.
madhan ravi
2019-9-22
编辑:madhan ravi
2019-9-22
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.
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.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
