Simple Question about Optimization of a FOR loop

1 Ansicht (letzte 30 Tage)
Mohsen am 14 Jun. 2013
How can I optimize (vectorize?) the following code in order to avoid the for loop and make the code to run faster?
for i=1:55;
A(i)=B((i-1)*3+1,1);
end
A is 1 x 55 and B is 165 x 1
Many thanks!
3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Laila Kazemi am 14 Jun. 2013
bc ur only assigning one raw to A
Mohsen am 14 Jun. 2013
You are both right!
A is 1 x 55
Do you know how to optimize the for loop? Thanks!

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Laila Kazemi am 14 Jun. 2013
C= B*3 +1 A (1:55, 1:55)= C(1:55, 1)
but the dimensions of ur matrices dont match , if B was 55 by 1 , it would have worked
3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Laila Kazemi am 14 Jun. 2013
actaully instead one make a matrix called D with D( , 1:165)=(1)
Mohsen am 16 Jun. 2013
Thanks for your answer but it doesn't seem to be the right answer... Please note that the multiplication by 3 and the addition by 1 is used to access the content of each element and not to modify the content of the elements...
I found this answer and it worked:
A=B(1:3:55*3,1);
Thanks again.

Melden Sie sich an, um zu kommentieren.

Andrei Bobrov am 14 Jun. 2013
A(1,:) = B((0:size(A,2)-1)*3+1);
1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Mohsen am 16 Jun. 2013
I found this answer and it worked:
A=B(1:3:55*3,1);
Thanks again.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Solver Outputs and Iterative Display finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by