Simple Question about Optimization of a FOR loop

1 次查看(过去 30 天)
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!

回答(2 个)

Laila Kazemi
Laila Kazemi 2013-6-14
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 个评论
Laila Kazemi
Laila Kazemi 2013-6-14
actaully instead one make a matrix called D with D( , 1:165)=(1)
Mohsen
Mohsen 2013-6-16
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.

请先登录,再进行评论。


Andrei Bobrov
Andrei Bobrov 2013-6-14
A(1,:) = B((0:size(A,2)-1)*3+1);
  1 个评论
Mohsen
Mohsen 2013-6-16
Thanks for your answer but it doesn't seem to be the right answer...
I found this answer and it worked:
A=B(1:3:55*3,1);
Thanks again.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by