How to store all answers from nested for loop?

How do I store the 36 answers of sumtotal(k) (from all of the for loop variations) into a matrix or array? I am only able to store the last run of the loop right now.(I think I would technically store it into a matrix. I don't understand the difference between matrix and array yet.)
Sorry, I have looked at multiple forums, online help videos, and reached out for other help. I am still confused on how to store the 36 answers I want. I can only store the last 'run' of the loop.
I appreciate your help!
Here is a simplified version of the code that I am working with:
a = [1 2 3]; b = [4 5 6];
for k = 1:min([numel(a) numel(b)])
for numboneplugs = 1:4;
for CTnum = [100 200 300];
suma(k) = a(k) + numboneplugs + CTnum + 1;
sumb(k) = b(k) + numboneplugs +CTnum + 1;
sumtotal(k) = suma(k) +sumb(k);
end;
end;
end;

Risposte (1)

The easiest way is to subscript them and create a 3D array:
a = [1 2 3]; b = [4 5 6];
CTnum = [100 200 300]
for k = 1:min([numel(a) numel(b)])
for numboneplugs = 1:4
for k3 = 1:numel(CTnum)
suma(k,numboneplugs,k3) = a(k) + numboneplugs + CTnum(k3) + 1;
sumb(k,numboneplugs,k3) = b(k) + numboneplugs + CTnum(k3) + 1;
sumtotal(k,numboneplugs,k3) = suma(k) +sumb(k);
end
end
end

7 Commenti

AWESOME! Thank you!
Your line 8 should say the following, right? I assume this was just a typo.
sumtotal(k,numboneplugs,k3) = suma(k,numboneplugs,k3) +sumb(k,numboneplugs,k3);
Thanks again!
As always, my pleasure!
‘Your line 8 should say the following, right?’
Yes, it should. I overlooked that change, and since the code I posted ran without error when I tested it, I did not catch it. My apologies.
Question: How do I convert this concept into my complicated code that has many code lines dependent on the previous code lines?
I don't know how often I need to carry over the (k), (k3), and (numofboneplugs) into the answer variable, and then carry that longer answer variable with all of the () into the next calculation line.
I hope this question makes sense.
Here's my code:
numPixelSpinefromPhan = [2688.00 3619.00 4306.00 4009.00 5029.00 5224.00 4151.00 2518.00]; numPixelOneBonePlug = [679 992.6 1192 1708.25 2250.833333 3054.75 4811 7401.5]; numPixelAllTissuefromWaterPhan = [113180 106638 109623 99278 99981 103614 105711 94184]; CTnumofspineat120kV = [755 758 780 813 767 755 661 561];
CTnumofboneplugat120kV = [230 320 415 455 792 870];
for k = 1:min([numel(numPixelSpinefromPhan) numel(numPixelOneBonePlug) numel(numPixelAllTissuefromWaterPhan) numel(CTnumofspineat120kV)]);
for numofboneplugs = 1:4;
for k3 = 1:numel(CTnumofboneplugat120kV);
CTnumoftissue = 0;
numPixelBonePlugs = (numofboneplugs*numPixelOneBonePlug(k));
numPixelSpine = numPixelSpinefromPhan(k);
numPixelBoneandSpine = numPixelBonePlugs + numPixelSpine;
numPixelSoftTissue = numPixelAllTissuefromWaterPhan(k) - (numPixelBoneandSpine);
numPixelAll = numPixelBoneandSpine + numPixelSoftTissue;
TotalCTnumofboneplugat120kV = numofboneplugs*CTnumofboneplugat120kV(k3)*numPixelBonePlugs;
TotalCTnumofspineat120kV = CTnumofspineat120kV(k)*numPixelSpine;
ct_bone = (TotalCTnumofboneplugat120kV*numPixelBonePlugs)+(TotalCTnumofspineat120kV*numPixelSpine);
ct_tissue = CTnumoftissue*numPixelSoftTissue;
ct_all = ct_bone + ct_tissue;
Percentage_Att_Bone = (ct_bone * numPixelBoneandSpine) / (ct_all * numPixelAll);
end
end
end
In your nested for loops, you are not subscripting any of the variables. I have no idea what you want to put in your 3D array.
I now understand that I don't need to type the (k) (k3) etc behind every variable since this just creates an array of the variable, which I really don't need. I only need the array for the final answer.
I am generating the 3D array. However, I am getting reoccurring numbers for the different CTnumofboneplugat120kV. I expect the numbers to change with this value.
Do you have any ideas why this is?
Thanks for all of the help! I appreciate it more than you can imagine!
I subscripted the Percentage_Att_Bone variable. However, my code is not calculating these numbers based off of the CTnumofboneplugat120kV whatsoever.
numPixelSpinefromPhan = [2688.00 3619.00 4306.00 4009.00 5029.00 5224.00 4151.00 2518.00]; numPixelOneBonePlug = [679 992.6 1192 1708.25 2250.833333 3054.75 4811 7401.5]; numPixelAllTissuefromWaterPhan = [113180 106638 109623 99278 99981 103614 105711 94184]; CTnumofspineat120kV = [755 758 780 813 767 755 661 561];
CTnumofboneplugat120kV = [230 320 415 455 792 870];
for k = 1:min([numel(numPixelSpinefromPhan) numel(numPixelOneBonePlug) numel(numPixelAllTissuefromWaterPhan) numel(CTnumofspineat120kV)]);
for numofboneplugs = 1:4;
for j = 1:numel(CTnumofboneplugat120kV);
CTnumoftissue = 0;
numPixelBonePlugs = numofboneplugs*numPixelOneBonePlug(k);
numPixelSpine = numPixelSpinefromPhan(k);
numPixelBoneandSpine = numPixelBonePlugs + numPixelSpine;
numPixelSoftTissue = numPixelAllTissuefromWaterPhan(k) - numPixelBoneandSpine;
numPixelAll = numPixelBoneandSpine + numPixelSoftTissue;
TotalCTnumofboneplugat120kV = numofboneplugs*[CTnumofboneplugat120kV(j)]*numPixelBonePlugs;
TotalCTnumofspineat120kV = CTnumofspineat120kV(k)*numPixelSpine;
ct_bone = (TotalCTnumofboneplugat120kV*numPixelBonePlugs)+(TotalCTnumofspineat120kV*numPixelSpine);
ct_tissue = CTnumoftissue*numPixelSoftTissue;
ct_all = ct_bone + ct_tissue;
Percentage_Att_Bone (j, numofboneplugs, k) = (ct_bone * numPixelBoneandSpine) / (ct_all * numPixelAll);
end
end
end
Your code runs for me without error.
You do not need the square brackets in this assignment:
TotalCTnumofboneplugat120kV = numofboneplugs*CTnumofboneplugat120kV(j)*numPixelBonePlugs;
They are not necessary, and slow your code. You are however addressing ‘CTnumofboneplugat120kV(j)’ correctly, so it should produce the result you want.
Be sure the units of all your variables produce the units you want in the calculated results. That is the easiest way to find any errors. This is tedious, however some symbolic calculation engines can do it for you.
I have no idea what you are doing or what your code is supposed to calculate. If it is not producing the results you want, you must troubleshoot that.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 31 Lug 2017

Commentato:

il 31 Lug 2017

Community Treasure Hunt

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

Start Hunting!

Translated by