Please suggest vectorization options for following code??

for Pos=3:1000
a_i_i(Pos:end,Pos)=a_sub(1:(end-Pos+2));
end

4 Comments

Rather than letting us figure out what your code does why don't you tell us?
Also, what is the content/size of a_i_i before the loop?
When you merely post completely undocumented code that you want to see vectorized, someone needs to first reverse-engineer your code. They need to thoroughly understand the code, what it does, why you are doing it. Then they need to find a way to do it more efficiently.
By explaining what you want the code to do, you help the person who might help you. That makes it easy for them to solve, as well as more likely that you will get a useful and timely response.
Thank you Guillaume & John D'Errico. Let me try to provide some more details about the problem. Please refer the image.
for Pos=3:10
a_i_i(Pos:end,Pos)=a_sub(1:(end-Pos+2));
end
I want to generate this matrix using second coloumn of this matrix which I have as a_sub. Using position i am defining the starting of nonzero value in coloumn. The matrix a_i_i is a 10x10 matrix. I am trying to vectorize it as its taking too much execution time.
Well, isn't that what my answer provides?

Sign in to comment.

 Accepted Answer

Assuming that a_i_i does not exist before the loop, or is just zeros, and assuming I understood correctly this completely uncommented and obscure code:
a_nopadding = toeplitz(a_sub(1:end-1), [a_sub(1), zeros(1, numel(a_sub)-2)]);
a_i_i = [zeros(2, size(a_nopadding, 2)+2); zeros(size(a_nopadding, 1), 2), a_nopadding]
If a_i_i is not empty before the loop, it's fairly trivial to adapt the above to fill it with the result.
And please, comment your code. Both your original code, and the above should have comments explaining the purpose.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!