Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

For loop help needed

조회 수: 1 (최근 30일)
Tom
Tom 2013년 11월 12일
마감: MATLAB Answer Bot 2021년 8월 20일
Can anyone see a reason why when I implement the following for loop:
for ashift = 1:n
x = [x(:,1) - x(ashift) x(:,2) - x(n+ashift) ...
x(:,3) - x(2*n+ashift)];
x = circshift(x,rowshift)
rowshift = rowshift - 1;
end
For the instance when n = 4, the output comes in the order of n = 1, n = 2, n = 4, n = 3 (I have checked this many times with manual calclations.
I can't think of any reason why this should be happening. Any help would be greatly appreciated.
Kind regards, Tom
  댓글 수: 5
Tom
Tom 2013년 11월 12일
편집: Tom 2013년 11월 12일
Yes x is a matrix, it has n columns and always 3 rows (but currently I am sticking with n = 4 for simplicity.
Each row of x represents the xyz coordinates of a vector, and essentially what I need is to place each vector in turn at the origin of coordinates (i.e. make all elements of its row 0) and shift every other vector by the same amount. Once this is done I am placing the vector at the origin at the top of the x matrix with circshift.
I am trying to do this for the 1st row, then the 2nd, 3rd and 4th in turn. But for some reason the 3rd and 4th runs are being switched.
Can you possibly see any reason this might be happening? Or alternatively (if it's not too much to ask) of a simpler way this could be done?
Thanks, Tom
Tom
Tom 2013년 11월 13일
rowshift is initialised to 0

답변 (1개)

Sean de Wolski
Sean de Wolski 2013년 11월 12일
Use the debugger!
  • Put this in a script or function and save the file.
  • Put a break point on the first line (click on the dash next to the line number)
  • Run the file and use dbstep or the Step icon to step through the loop and investigate what is happening on each line during each iteration.
  댓글 수: 4
Tom
Tom 2013년 11월 12일
Sorry yes I have now managed to use the debugger, but unfortunately still can't find the problem.
Sean de Wolski
Sean de Wolski 2013년 11월 12일
On each line run the line at the command prompt and figure out what each value is doing.
For example
x(:,1) - x(ashift) x(:,2) - x(n+ashift) x(:,3) - x(2*n+ashift)]
Notice I took the ';' off the end so it prints.
Not lets look inside at it
x(n+ashift)
Divide and conquer until you figure out what's going on.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by