Vectorizing a for loop?

조회 수: 1 (최근 30일)
Christopher
Christopher 2013년 7월 11일
How would I rewrite this code using say repmat to vectorize it and get rid of the for loop? I have been playing with this, but cannot figure it out. All I am doing is solving the ss2 equation while increasing ILAT. Thank you
A1=[0 0 0];
B1=[-5 5 0];
npan2=3
for ILAT=1:npan1+1
ss2=A1+(B1-A1)/(npan2)*((ILAT)-1);
mat(ILAT,:)=ss2
end

채택된 답변

dpb
dpb 2013년 7월 11일
편집: dpb 2013년 7월 11일
For your A1, B1 and npan1=2,
>> bsxfun(@times,A1+(B1-A1),[0:npan1]'/npan2)
ans =
0 0 0
-1.6667 1.6667 0
-3.3333 3.3333 0
>>
Agrees w/ your for..end loop
OK, I add the two blanks and took out the ' MATL'...happy, now, Jan? :)
Now if TMW would just fix it so that NO blanks means MATLAB code and two blanks for wordwrap, how much easier life would be...or at least this one little corner of it... :)
  댓글 수: 6
Jan
Jan 2013년 7월 11일
@dpb: I really like this misunderstanding! And I can confirm that many users are confused by the formatting in the forum. Other forums, which use e.g. BBCode like [code]y=1[/code] have less problems to encourage beginners to use this feature. Even the "{} Code" button is not attractive enough and offers even a special feature: If you hit it without selecting code before, it inserts a magic "if true... end". I have been very confused about finding this so often in posted code, because I never tried to hit the button without a selection.
I've asked many users before, why they do not format their code properly, because I want to find out, how this could be improved. But your answer has been the best and I appreciate it, that you find your own working solution to interprete the instructions about the strange formatting methods.
So, dpb, you made my day.
dpb
dpb 2013년 7월 11일
Perhaps there's a better way to write the help that makes it abundantly clear what's meant--I don't know if my confusion has anything to do w/ anybody else's or not but when I see something that says "markup syntax" I expect it to be syntax. If the key phrase is "two leading blanks" then that's what the syntax is; not " MATLAB code" (w/ two leading blanks).
I still say if TMW wants well-formed code they need it to simply default to that format because the casual user and newbie isn't going to (as has been amply demonstrated) take the time/effort to try to clean up after themselves.
Anyway, Jan, glad to have brought some cheer to somebody along the way... :)
I promised TMW in return for the R2012b upgrade that I'd try to support Answers forum some and supply feedback on it as well as on the product. I don't know how much longer my patience is going to hold though as well as the amount of time it's taken because of the interface. I'm pretty sure I'll be back to cs-sm exclusively before long because of the time issue, though.

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

추가 답변 (2개)

Christopher
Christopher 2013년 7월 11일
Thank you very much sir! I am still trying to understand bsxfun, as I am relatively new to this command.
  댓글 수: 2
Cedric
Cedric 2013년 7월 11일
Here is a basic, asymmetric example to illustrate:
>> bsxfun(@minus, (1:5).', (1:4))
ans =
0 -1 -2 -3
1 0 -1 -2
2 1 0 -1
3 2 1 0
4 3 2 1
Jan
Jan 2013년 7월 11일
@Cristopher: Please do not post comments in the answers section.

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


Andrei Bobrov
Andrei Bobrov 2013년 7월 11일
out = (0:npan1).'*(A1+(B1-A1)/npan2);

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by