Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Help with vectorization of my code

3 visualizzazioni (ultimi 30 giorni)
Saipraneeth
Saipraneeth il 3 Lug 2016
Chiuso: MATLAB Answer Bot il 20 Ago 2021
Hello all. I have a code like below
cd = [21543;21544;21545;21546;21547;21548;21549];
for i=1:7
K(2*cd(i)-1,2*cd(i)-1)= K(2*cd(i)-1,2*cd(i)-1) + St(1,1,i);
K(2*cd(i)-1,2*cd(i))= K(2*cd(i)-1,2*cd(i)) + St(1,2,i);
end
K is a sparse matrix of size [44044 x 44044] and 'St' is a 3D array. I've tried to vectorize this code with no success. Could anyone please help?
Thanks.
  3 Commenti
Saipraneeth
Saipraneeth il 4 Lug 2016
Thanks for the reply. I named it 'cd' just for writing it here, it's different in my actual code.
Actually, the problem is this code is inside another loop with around 20 iterations. The 'cd' vector changes in every iteration and becomes big like (9000 x 1), which is the maximum size 'i' will take. So, it does take a while and I am trying to reduce it. Is there any way to vectorize it?
Walter Roberson
Walter Roberson il 4 Lug 2016
... do not allow it to grow in size. Assign it the maximum size before the loop. It will speed up your code a lot.

Risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by