필터 지우기
필터 지우기

Vectorizing inside a loop

조회 수: 1 (최근 30일)
Avery
Avery 2013년 3월 20일
I am trying to implement something like this
for i=1:n
matrix(i:n:end,4:6)=y(i,:)-matrix(i,1:3);
end
I was under the impression that vectorizing would allow me to eliminate the need for another loop but I receive 'Subscripted assignment dimension mismatch' when I attempt to evaluate this. I believe it is because it is not assigning it on a per row basis as I had hoped.
Is it at all possible to accomplish this without adding more loops?
  댓글 수: 3
Avery
Avery 2013년 3월 20일
y is an n by 3 matrix. n changes, hence it being a variable, as do the size of y and matrix. I understand that the left is a matrix and the right is a vector, but my understanding of vectorization leads me to believe that I can replace a loop in this way.
ChristianW
ChristianW 2013년 3월 20일
Show your working double loop.

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

답변 (1개)

Babak
Babak 2013년 3월 20일
The problem with
matrix(i:n:end,4:6)=y(i,:)-matrix(i,1:3);
is that the first index i:n:end is a vector and does not correspond to 1 element. On the other hand, on the righe side, the index i is a sinle element which chooses the row with index i.
You need to change i:n:end to a proper single index accordign to what you want to write to. If you give a numerical example, maybe I can figure what you really want to do.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by