Hey, i have a problem with writing a loop, which will be able to do this:
phi(1)=a_locs(1)/((a_odd(1)));
phi(2)=a_locs(2)/((a_odd(1)));
phi(3)=a_locs(3)/((a_odd(1)));
phi(4)=a_locs(4)/((a_odd(1)));
phi(5)=(a_locs(5)-a_odd(1))/((a_odd(2)));
phi(6)=(a_locs(6)-a_odd(1))/((a_odd(2)));
phi(7)=(a_locs(7)-a_odd(1))/((a_odd(2)));
phi(8)=(a_locs(8)-a_odd(1))/((a_odd(2)));
phi(9)=(a_locs(10)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
phi(10)=(a_locs(11)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
Someone can help me?

댓글 수: 7

James Tursa
James Tursa 2015년 4월 3일
편집: James Tursa 2015년 4월 3일
Why do you want a loop? Is your real problem actually bigger than this? Is everything shown variables, or are some of them functions?
Michael Monka
Michael Monka 2015년 4월 3일
All are variables, and i have to evaluate a large number (450) of equations, which looks as i write before, and i thought it is possible to write (one or two?) loops which will be do that thing...?
James Tursa
James Tursa 2015년 4월 3일
So what are the sizes of the variables involved, and can you be more explicit in the pattern of the calculations for these larger sizes?
Image Analyst
Image Analyst 2015년 4월 3일
The thing I can't figure out is why a_locs(9) is totally missing from any of the right hand side expressions. And there are not enough equations to figure out which elements need to be "skipped" in the other equations, and what equations the "skipping" would happen at.
Stephen23
Stephen23 2015년 4월 3일
It seems like this might be able to be written using vectorized code, thus avoiding any loops... but it would be nice to know the general algorithm here.
Okay, so the size of variable which i have to obtain (which is phi) is 450. The pattern of the calculations goes like:
phi(1)=a_locs(1)/((a_odd(1)));
phi(2)=a_locs(2)/((a_odd(1)));
phi(3)=a_locs(3)/((a_odd(1)));
phi(4)=a_locs(4)/((a_odd(1)));
phi(5)=(a_locs(5)-a_odd(1))/((a_odd(2)));
phi(6)=(a_locs(6)-a_odd(1))/((a_odd(2)));
phi(7)=(a_locs(7)-a_odd(1))/((a_odd(2)));
phi(8)=(a_locs(8)-a_odd(1))/((a_odd(2)));
phi(9)=(a_locs(9)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
phi(10)=(a_locs(10)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
phi(11)=(a_locs(11)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
phi(12)=(a_locs(12)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
phi(13)=(a_locs(13)-(a_odd(1)+a_odd(2)+a_odd(3)))/((a_odd(4)));
phi(14)=(a_locs(14)-(a_odd(1)+a_odd(2)+a_odd(3)))/((a_odd(4)));
phi(15)=(a_locs(15)-(a_odd(1)+a_odd(2)+a_odd(3)))/((a_odd(4)));
phi(16)=(a_locs(16)-(a_odd(1)+a_odd(2)+a_odd(3)))/((a_odd(4)));
phi(17)=(a_locs(17)-(a_odd(1)+a_odd(2)+a_odd(3)+a_odd(4)))/((a_odd(5)));
phi(18)=(a_locs(18)-(a_odd(1)+a_odd(2)+a_odd(3)+a_odd(4)))/((a_odd(5)));
phi(19)=(a_locs(19)-(a_odd(1)+a_odd(2)+a_odd(3)+a_odd(4)))/((a_odd(5)));
phi(20)=(a_locs(20)-(a_odd(1)+a_odd(2)+a_odd(3)+a_odd(4)))/((a_odd(5)));
phi(21)=...
Jan
Jan 2015년 4월 3일
@Michael: It looks like phi consists of blocks of the length 4. But 450 is not evenly divisable by 4?

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

 채택된 답변

Jan
Jan 2015년 4월 3일
편집: Jan 2015년 4월 3일

1 개 추천

With a loop:
phi = zeros(1, 452); % Instead of 450?
s = 0;
m = 1;
for k = 1:4:452
phi(k:k+4) = (a_locs(k:k+4) - s) / a_odd(m);
s = s + a_odd(m);
m = m + 1;
end
And vectorized - assuming that a_odd is a row vector:
phi = reshape(a_locs, 4, 113);
phi = bsxfun(@minus, phi, [0, cumsum(a_odd(1:112)]);
phi = bsxfun(@rdivide, phi, a_odd(1:113));
phi = reshape(phi, 1, 452);

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 4월 3일

편집:

Jan
2015년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by