When does one need to index for plotting
이전 댓글 표시
I refer to this question:
I previously never indexed when I filled with values in a while loop and the values were saved in the vector.
But in that case I needed to do :
iterations(iter) = iter
error(iter) = norm(x-x_ref)
to make sure the values were saved in the vector and adapt automatically.
Can someone explain why is that, when manipulating vectors, sometimes you need to iterate with an index and sometimes not?
채택된 답변
추가 답변 (1개)
am
2019년 3월 28일
0 개 추천
댓글 수: 5
Walter Roberson
2019년 3월 28일
You can click on "Comment on this Answer" to add a comment, instead of creating a new Answer.
Walter Roberson
2019년 3월 28일
In my example above https://www.mathworks.com/matlabcentral/answers/452966-when-does-one-need-to-index-for-plotting#comment_687140 with x = x + sin(2*pi*t*K) then, Yes, each iteration rewrites the entire vector x. The example shows that it is not itself looping that is the key as to whether you need to index, but rather whether you are working with the whole variable (no indexing) or only part of the variable (indexing needed)
Another example:
x = randn(15, 2);
x(:,1) = sin(x(:,1));
x(:,2) = exp(x(:,2));
No looping, and multiple elements are accessed, but indexing was required because only part of the variable is being affected.
am
2019년 3월 28일
Walter Roberson
2019년 3월 28일
The first line creates a 15 x 2 matrix of normally distributed random numbers (with mean 0 and standard deviation 1)
The second line calculates the sine of the first column and replaces the first column with those sines. Indexing was needed on the right side of the = in order to fetch only the first column, and indexing was needed on the left side of the = in order to assign to only the first column.
The third line calculates the exponential of the second column and replaces the second column with those exponentials. Indexing was needed on the right side of the = in order to fetch only the second column, and indexing was needed on the left side of the = in order to assign to only the second column.
The end result is 15 x 2.
am
2019년 3월 28일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!