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.

Antworten (3)

Poongavanam Palani
Poongavanam Palani am 9 Okt. 2019

1 Stimme

This should help!
P = [];
for j = 1:15
percentage_change = (x((8+j),k)-baseline1/baseline1*100;
P = [P, percentage_change,];
end
Image Analyst
Image Analyst am 7 Mär. 2014

0 Stimmen

P = percentage_change;
Of course after you do that P is no longer empty.

18 Kommentare

Image Analyst
Image Analyst am 7 Mär. 2014
Bearbeitet: Image Analyst am 7 Mär. 2014
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
Thang Le am 7 Mär. 2014
Thank you for your quick response. I did indeed want to have all the values of percentage_change in P. For some reason, when I tried what you suggested, I only got the last percentage_change value in for the last row. Every other row is still 0. Do you happen to know why?
This is not related but I did have to change j=1:16 since 0 would give me problem with the subscript.
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
Looking at the script, I'd say it should work. But when I run it with real data, although it does give me all 16 distinct values of percentage_change in the command window, only the 16th value got put into P. P(1:15,1) gives me 15 zeros.
for j=0:15
P = zeros(16, 1);
percentage_change = (x((8+j),k)-baseline1)/baseline1*100;
P(j+1, :) = percentage_change;
end
Image Analyst
Image Analyst am 7 Mär. 2014
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
Thang Le am 7 Mär. 2014
x is a 200x2 double. Here is the script and the data I use for testing the script. Thank you for helping me out with this (and not for the first time either)!
Image Analyst
Image Analyst am 8 Mär. 2014
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
Thang Le am 8 Mär. 2014
There should be 200 rows and 2 columns (when I do uiopen in Matlab or Libreoffice I see all the rows and columns but csvread only gives me one column with 200 rows. Maybe the blank columns in between create the issue. Nevertheless, we I do load(roi_beta), everything is there).
Anyway, each row is a volume and each column is an ROI. These are the beta values and I am calculating time courses. There are 12 blocks, each block has 16 volumes (we scrap 8 volumes). The script is supposed to take these 200 volumes and figure out the baseline and percentage change for each block. That's why you see 12 baselines in the script. There is still a fair amount of work to be done as you can see. I'm currently stuck on this percentage_change as we have discussed.
Image Analyst
Image Analyst am 8 Mär. 2014
It's not a proper csv file because there's no commas separating the variables. Please fix it first.
Thang  Le
Thang Le am 8 Mär. 2014
I did have an error in my script while creating the csv file. Here is the proper csvfile. I've used this and the issue persists.
Image Analyst
Image Analyst am 8 Mär. 2014
OK, that's fixed, but I can't run your code. What is the spm function? Is it necessary?
Thang  Le
Thang Le am 8 Mär. 2014
Did you mean the spm_select? It's just a file selector. I suppose we could use something else to select the .csv file without using the spm_select.
I can't run this function
spm('Defaults','fMRI');
Thang  Le
Thang Le am 8 Mär. 2014
This is to turn on spm8. You can take that out but if you do, you'll have to use a different file selector (instead of spm_select).
Image Analyst
Image Analyst am 8 Mär. 2014
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
Thang Le am 8 Mär. 2014
I'm so sorry! My inexperience with scripting is indeed making this very difficult. The spm_select is supposed to go to the appropriate folder then select the .csv file that I have uploaded on here. That's all it does. Without it, the load function would not work.Here is the spm_select.m in case you want to use/see it.
Image Analyst
Image Analyst am 8 Mär. 2014
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
Thang Le am 8 Mär. 2014
I will. Thank you for all your help and patience, especially on a Friday and Saturday. Have a great weekend!

Melden Sie sich an, um zu kommentieren.

Jacques
Jacques am 8 Mär. 2014

0 Stimmen

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 Kommentare

Image Analyst
Image Analyst am 8 Mär. 2014
In MATLAB, type control-a then control-i. Then paste in here.
Nathanael Ferster
Nathanael Ferster am 30 Sep. 2021
This helped me too, thanks.
Image Analyst
Image Analyst am 30 Sep. 2021
@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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 7 Mär. 2014

Kommentiert:

am 30 Sep. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by