# Vectorizing a for loop?

1 view (last 30 days)
Christopher on 11 Jul 2013
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 on 11 Jul 2013
Edited: dpb on 11 Jul 2013
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
>>
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... :)

dpb on 11 Jul 2013
Paragraph Markup Use the following markup text for entire paragraphs. Use one completely blank line (two newlines) to separate paragraphs. ... Description MATLAB code (requires two leading spaces) ...
That's all I knew/went by to get code to not linewrap.
OK, now I guess I understand that by "MATLAB code" they didn't mean a markup tag of that string (which I thought really, really, klunky, but hey, what the hey?) but actually have to insert two extra spaces in front of the code. OK, so I misread it...it certainly wasn't clear that it wasn't a tag to me, anyway, when I went searching for the magic elixir to quit having to use mouse and poke at buttons.
Jan on 11 Jul 2013
@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 on 11 Jul 2013
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.

Christopher on 11 Jul 2013
Thank you very much sir! I am still trying to understand bsxfun, as I am relatively new to this command.

Cedric Wannaz on 11 Jul 2013
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 on 11 Jul 2013

Andrei Bobrov on 11 Jul 2013
out = (0:npan1).'*(A1+(B1-A1)/npan2);