How to store all answers from nested for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
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;
댓글 수: 0
답변 (1개)
Star Strider
2017년 7월 31일
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
Star Strider
2017년 7월 31일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Support Package for Raspberry Pi Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!