hi!
I would like to get some help to vectorize this 'for' loop:
for k = 1:length(tempStructs1{1,1})
sPoint_ePoint =[ tempStructs1{1,1}(k).point1 ; tempStructs1{1,1}(k).point2 ];
plot(sPoint_ePoint(:,1),sPoint_ePoint(:,2),'LineWidth',2,'Color','blue');
end
sPoint_ePoint is a column vector of 2 cells. Each cell contains one point. those 2 points represents a line that i want to plot.
I tried to vectorize the loop but i'm stuck... I can't make the program plot the lines appropriately, i think that happens because there is no 'k' (the looping variable) in this line of code:
plot(sPoint_ePoint(:,1),sPoint_ePoint(:,2),'LineWidth',2,'Color','blue');
and one more question: How can I know when the vectorized loop ends? there is no 'end' statement...
thanks!

댓글 수: 1

Jan
Jan 2015년 12월 21일
The question does not make sense. There is an end statement. In addition "vectorizing" and "loops" are complementary, so it is not meaningful to ask for the "end of the vectorized loop".

댓글을 달려면 로그인하십시오.

 채택된 답변

Jan
Jan 2015년 12월 21일

0 개 추천

Vectorizing is useful, when the values of arrays are processed. Then operations on the arrays are faster than processing the array in chunks or elementwise in a loop. But in your case the bottleneck is the plot command. Although plot can operate on matrices also, this seems not applicable in your case.
Some simplifications might improve the readability of the code:
data = tempStructs1{1,1};
for k = 1:length(data)
plot(data(k).point1, data(k).point2, 'LineWidth', 2, 'Color', 'b');
end

추가 답변 (1개)

chk2
chk2 2015년 12월 21일

0 개 추천

thanks for the fast answer!

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 12월 21일

답변:

2015년 12월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by