필터 지우기
필터 지우기

Need help with a nested for loop

조회 수: 3 (최근 30일)
Joel Coburn
Joel Coburn 2015년 5월 30일
댓글: Image Analyst 2015년 5월 30일
I have
L = 28x1 vector
D = 28x1 vector
My equation is: V = L*((pi*((D+0.064).^2)/4)-(pi*D.^2)/4)
I have tried
for n = 1:length(D1)
for m = 1:length(L1)
TankVol(n,m) = L1(m,:)*((pi*((D1(n,:)+0.064).^2)/4)-(pi*D1(n,:).^2)/4);
end
end
But it's not giving what I need
It's going through the loop too many times if that makes sense, I basically need a V value for each value in L and D.

채택된 답변

Image Analyst
Image Analyst 2015년 5월 30일
편집: Image Analyst 2015년 5월 30일
If there is a value of D1 for every value of L1 and vice versa, what about:
L1 = rand(28, 1); % 28x1 vector
D1 = rand(28, 1); % 28x1 vector
% My equation is: V = L*((pi*((D+0.064).^2)/4)-(pi*D.^2)/4)
for n = 1:length(D1)
for m = 1:length(L1)
TankVol(n,m) = L1(m)*((pi*((D1(n)+0.064).^2)/4)-(pi*D1(n).^2)/4);
end
end
imshow(TankVol, []);
Or, if you want to take the nth element of D1 at the same time that you take the nth element of L1:
L1 = rand(28, 1); % 28x1 vector
D1 = rand(28, 1); % 28x1 vector
% My equation is: V = L*((pi*((D+0.064).^2)/4)-(pi*D.^2)/4)
TankVol = L1 .* ((pi*((D1+0.064) .^ 2)/4)-(pi*D1 .^ 2)/4);
plot(TankVol, 'b*-');
grid on;
  댓글 수: 2
Joel Coburn
Joel Coburn 2015년 5월 30일
Worked perfectly! Thanks so much
Image Analyst
Image Analyst 2015년 5월 30일
Just curious - which version did you use?

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Murali Krishna
Murali Krishna 2015년 5월 30일
In matlab u need not use loop to access each element. Try this
V = L.*((pi*((D+0.064).^2)/4)-(pi*D.^2)/4)
result will be stored in v as column matrix

Walter Roberson
Walter Roberson 2015년 5월 30일
t = ((pi*((D+0.064).^2)/4)-(pi*D.^2)/4);
V = bsxfun(@times, L, t.');

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by