Vectorizing a for loop?
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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
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
2013-7-11
2 个评论
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!