How do I tell MATLAB to run through the for loop with 3 varying variables?
The following is my current MATLAB code:
for 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];
%
for numofboneplugs = 1:4;
for CTnumofboneplugat120kV = 230;
%320 415 455 792 870];
CTnumoftissue = 0;
%
numPixelBonePlugs = (numofboneplugs*numPixelOneBonePlug);
numPixelSpine = numPixelSpinefromPhan;
numPixelBoneandSpine = numPixelBonePlugs + numPixelSpine;
numPixelSoftTissue = numPixelAllTissuefromWaterPhan - (numPixelBoneandSpine);
numPixelAll = numPixelBoneandSpine + numPixelSoftTissue;
%
TotalCTnumofboneplugat120kV = numofboneplugs*CTnumofboneplugat120kV*numPixelBonePlugs;
TotalCTnumofspineat120kV = CTnumofspineat120kV*numPixelSpine;
%
ct_bone = (TotalCTnumofboneplugat120kV*numPixelBonePlugs)+(TotalCTnumofspineat120kV*numPixelSpine);
ct_tissue = CTnumoftissue*numPixelSoftTissue;
ct_all = ct_bone + ct_tissue;
%
Percentage_Att_Bone(CTnumofboneplugat120kV) = (ct_bone * numPixelBoneandSpine) / (ct_all * numPixelAll);
end
end
end

 Respuesta aceptada

James Tursa
James Tursa el 28 de Jul. de 2017
Editada: James Tursa el 28 de Jul. de 2017
E.g., to vary them at the same time
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];
n = numel(numPixelSpinefromPhan);
for k=1:n
% Use numPixelSpinefromPhan(k), numPixelOneBonePlug(k), numPixelAllTissuefromWaterPhan(k)
end

4 comentarios

Justine Schneider
Justine Schneider el 29 de Jul. de 2017
Editada: James Tursa el 29 de Jul. de 2017
What about for the variables numPixelOneBonePlug and numPixelAllTissuefromWaterPhan? Also, there is an error in my original code. I also need to initially tell the code that:
CTnumofspineat120kV = [787 800 800 800 756 721 777 767];
Also, I am getting an error in my code with the following lines because the matrix dimensions do not match when trying to multiple in later lines of code.
n = numel(numPixelSpinefromPhan);
n = numel(numPixelOneBonePlug);
n = numel(numPixelAllTissuefromWaterPhan);
n = numel(CTnumofspineat120kV);
for k=1:n
numPixelSpinefromPhan(k);
numPixelOneBonePlug(k);
numPixelAllTissuefromWaterPhan(k);
CTnumofspineat120kV(k);
end
James Tursa
James Tursa el 29 de Jul. de 2017
If the dimensions don't match, then you need to tell us how you intend to use these variables inside your loop. I.e., if they are not the same size then you can't vary them all at the same time with one loop variable. So, what is your intent on how they all get used within the loop(s)?
I don't know how to vocalize in MATLAB language how I intend to use these variables inside of my loop. However, I can show you and explain what I am trying to do as best as possible.
I want to use one entry from each of the four variable vectors in the first line of code to calculate the Percentage_Att_Bone (& store it in an array) for each for loop. The first loop would be based on all of the first entries in the variable vectors. The second loop would be based on all of the second entries in the variable vectors, etc.
I am still having a tough time with this numel function to have it index all four variable vectors for each for loop. I know how to operate numel with one variable, but not with four variables.
How do I use numel with four variables?
I hope my question is clear.
Here is the code I am working with:
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];
n = numel(numPixelSpinefromPhan numPixelOneBonePlug numPixelAllTissuefromWaterPhan CTnumofspineat120kV);
for k = 1:n;
for numofboneplugs = 1:4;
for CTnumofboneplugat120kV = [230 320 415 455 792 870]; CTnumoftissue = 0;
numPixelBonePlugs = (numofboneplugs*numPixelOneBonePlug);
numPixelSpine = numPixelSpinefromPhan;
numPixelBoneandSpine = numPixelBonePlugs + numPixelSpine;
numPixelSoftTissue = numPixelAllTissuefromWaterPhan - (numPixelBoneandSpine);
numPixelAll = numPixelBoneandSpine + numPixelSoftTissue;
TotalCTnumofboneplugat120kV = numofboneplugs*CTnumofboneplugat120kV*numPixelBonePlugs;
TotalCTnumofspineat120kV = CTnumofspineat120kV*numPixelSpine;
ct_bone = (TotalCTnumofboneplugat120kV*numPixelBonePlugs)+(TotalCTnumofspineat120kV*numPixelSpine);
ct_tissue = CTnumoftissue*numPixelSoftTissue;
ct_all = ct_bone + ct_tissue;
Percentage_Att_Bone(CTnumofboneplugat120kV) = (ct_bone * numPixelBoneandSpine) / (ct_all * numPixelAll);
end
end
end
Justine Schneider
Justine Schneider el 31 de Jul. de 2017
I got the numel function to work with all four variables after experimenting with other things. Thanks for all of the help!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Jul. de 2017

Comentada:

el 31 de Jul. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by