For loop help needed
Información
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
Mostrar comentarios más antiguos
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 comentarios
Azzi Abdelmalek
el 12 de Nov. de 2013
Your problem is not clear
Walter Roberson
el 12 de Nov. de 2013
Your x appears to be a matrix, but you are access x(ashift) and x(n_ashift) and x(2*n+ashift), which are linear accesses. Are you sure you want to be switching between 2D coordinates and 1D coordinates in the same expression? If x is n rows down, then the 1D coordinates correspond to x(ashift, 1), x(ashift, 2) and x(ashift, 3) which would be clearer to read.
Walter Roberson
el 12 de Nov. de 2013
What do you initialize rowshift to ?
Tom
el 13 de Nov. de 2013
Respuestas (1)
Sean de Wolski
el 12 de Nov. de 2013
0 votos
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 comentarios
Tom
el 12 de Nov. de 2013
Walter Roberson
el 12 de Nov. de 2013
Sean did give step by step instructions. Notice the "click on the dash next to the line number", and "dbstep or the Step icon"
Tom
el 12 de Nov. de 2013
Sean de Wolski
el 12 de Nov. de 2013
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.
La pregunta está cerrada.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!