Transpose... Not Transposing!
Mostrar comentarios más antiguos
Hello Community,
I have an annoying glitch in some code, in that there is a line in my script that tells some data that has just been created in a for loop and stored to a struct to transpose. However, sometimes it will transpose and the subsequent output from the script works, and other times it doesn't transpose, and I get thrown a "matrix dimensions must agree" error! The code is:
% Calculate mean for all VLM CA ranges in the 'ZS.' struct
for i = 1:N
ZS.vlmcamean(i) = mean(ZS.ZSFLCA.(sprintf('ZSca%d',i)));
end
% Transpose the VLM CA mean ('vlmcamean') field
ZS.vlmcamean = ZS.vlmcamean';
Where you can clearly see the crucial " ZS.vlmcamean' " to transpose the data. The reason I do this is to make the data be the correct size for a function that follows in the script.
Can anyone suggest a workaround or advise on how to have this transpose element consistently do what it is meant to?
Many thanks.
10B.
3 comentarios
the cyclist
el 25 de Nov. de 2016
Can you post an example that we can run, that distills this into the simplest possible case that replicates the error? (I find that this process itself will often expose the problem.)
10B
el 25 de Nov. de 2016
Respuesta aceptada
Más respuestas (2)
dbmn
el 25 de Nov. de 2016
I suggest to consider seeing the problem from another perspective.
Basically what you want is not the transpose of your input but the "right" format of it (and the best part is, that you already know how it should look like). So my suggestion is to use something like
ZS.vlmcamean = reshape(ZS.vlmcamean, [max(size(ZS.vlmcamean)), min(size(ZS.vlmcamean))]);
Maybe due to some unforeseen reason your ZS.vlmcamean looks differently in the loop. And the solution will make it always look the same.
1 comentario
10B
el 25 de Nov. de 2016
dpb
el 25 de Nov. de 2016
I'm quite certain Matlab is not "not transposing"; symptom sounds to me as though likely you're alternating operating on a row or column vector depending upon which was last left in the workplace the previous iteration; Matlab will retain existing array orientation when addressing it with single subscript.
Either be sure to clear the new accumulator when starting fresh so the default behavior will occur each time or alternatively, you could ensure the orientation by adding the second dimension subscript explicitly.
for i=1:N
newVar(i,1)=whatever; % create as column vector by dynamic allocation
...
newVar=zeros(N,1); % preallocate first as column vector (recommended)
for i=1:N
newVar(i)=... % address existing column vector
Doing this will ensure orientation and as side benefit eliminate an extra transpose step plus is much more efficient to preallocate memory.
5 comentarios
dpb
el 25 de Nov. de 2016
As said, because by default ML in loop will create the row vector but if it exists after you've transposed it, will retain that shape which you then transpose again.
The above will definitely create a column vector unless and until you change it again somehow but you didn't show the code as modified nor the input data so can't tell where you went wrong...use the debugger and step through and see where you have the logic error.
10B
el 25 de Nov. de 2016
Nancy Hammond
el 13 de Jul. de 2021
Editada: Nancy Hammond
el 13 de Jul. de 2021
I'm having same problem.
WHEN I RUN LINES 374 AND 400 SEPARATELY:
line 374 sets rnt = log(1+cumret_a):
rnt = log(1+cumret_a);
size(rnt)
ans = 1 90
line 400 is function of transpose of rnt:
svt_x = [ NaN; -vt(2:end)+vt(1:end-1)+rnt(2:end)'-inflt(2:end)-grt(2:end)];
size(rnt)
ans =1 90
BUT GET ERROR WHEN RUN ENTIRE CODE:
Error in get_data_and_run_var_nh (line 402)
svt_x = [ NaN; -vt(2:end)+vt(1:end-1)+rnt(2:end)'-inflt(2:end)-grt(2:end)]; % use to check
identities.
>> size(rnt)
ans = 90 1
This occurs multiple times in this code from a completed paper by a prominent academic. Example:
line 88 omv_a = omv_a'; 90 by 1
line 166: ovy_a = omv_a./py_a; size 90 by 90 because
Now omv_a is 1 by 90, while py_a is 90 by 1
Image Analyst
el 13 de Jul. de 2021
@Nancy Hammond, can you please attach your data and code in a new question? Put the variables into a .mat file and attach with the paper clip icon.
save('answers.mat', 'cumret_a', 'rnt', 'vt'); % etc. for all the variables we need to run those lines of code.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!