Add values to a matrix from a loop
이전 댓글 표시
Hi,
I have the following lines in my script:
for j=0:15;
percentage_change = (x((8+j),k)-baseline1)/baseline1*100;
end
The above gives me 16 different values. Could anyone please tell me how I would go about putting all these 16 values in an empty 16x1 matrix called P? Thank you so much for your help.
답변 (3개)
Poongavanam Palani
2019년 10월 9일
This should help!
P = [];
for j = 1:15
percentage_change = (x((8+j),k)-baseline1/baseline1*100;
P = [P, percentage_change,];
end
Image Analyst
2014년 3월 7일
P = percentage_change;
Of course after you do that P is no longer empty.
댓글 수: 18
Image Analyst
2014년 3월 7일
편집: Image Analyst
2014년 3월 7일
If you want to store all the percentage_change, and k is a list of 16 indexes, so that percentage_change is a row vector, then
P = zeros(16, 16)
for j=0:15
percentage_change = (x((8+j),k)-baseline1)/baseline1*100;
P(j, :) = percentage_change;
end
Thang Le
2014년 3월 7일
Image Analyst
2014년 3월 7일
You need P(j+1,:) because you can't have the 0th row. See if this works for you:
x = rand(1000, 50);
k=1:16;
P = zeros(16, 16)
baseline1 = 100;
for j=0:15
percentage_change = (x((8+j),k)-baseline1)/baseline1*100;
P(j+1, :) = percentage_change;
end
P
Thang Le
2014년 3월 7일
Image Analyst
2014년 3월 7일
It runs with the sample data x I created. The only way to figure it out is to upload your data. Also, what does "whos x" report? And what are you using for k and baseline1?
Thang Le
2014년 3월 7일
Image Analyst
2014년 3월 8일
I used csvread() to read this into x, and x is not a N rows by 16 column array. There are 2001 rows and only 1 column, not 16. So, what's going on?
Thang Le
2014년 3월 8일
Image Analyst
2014년 3월 8일
It's not a proper csv file because there's no commas separating the variables. Please fix it first.
Thang Le
2014년 3월 8일
Image Analyst
2014년 3월 8일
OK, that's fixed, but I can't run your code. What is the spm function? Is it necessary?
Thang Le
2014년 3월 8일
Image Analyst
2014년 3월 8일
I can't run this function
spm('Defaults','fMRI');
Thang Le
2014년 3월 8일
Image Analyst
2014년 3월 8일
Well now I've got to leave for a few hours. Thang, you're not making it easy to help you and I'm not sure why. What value should I assign to spm_select to get it to demonstrate your problem? If you give me a line of code to assign spm_select then I might try it when I get back.
Thang Le
2014년 3월 8일
Image Analyst
2014년 3월 8일
Since I can't run your code. I really recommend you take the same actions I would take to solve your issues. And that is to carry out the actions you will learn in this video: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Thang Le
2014년 3월 8일
Jacques
2014년 3월 8일
P = zeros(16,1);
for j=0:15;
percentage_change = (x((8+j),k)-baseline1)/baseline1*100;
P(j+1) = percentage_change;
end
Sorry for not indenting code, I don't know how to do that...
댓글 수: 3
Image Analyst
2014년 3월 8일
In MATLAB, type control-a then control-i. Then paste in here.
Nathanael Ferster
2021년 9월 30일
This helped me too, thanks.
Image Analyst
2021년 9월 30일
@Nathanael Ferster, glad it helped. Maybe it's a little known trick. If more people would type control-a then control-i in MATLAB before pasting in here, then it would sure make the code easier to follow. Of course after they paste it in, they still need to highlight it and click the Code icon to format it as code and enable the "copy" button so that people can copy the entire code with one click.
카테고리
도움말 센터 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!