Error when building a array code

1 visualización (últimos 30 días)
desert_scientist90 el 25 de Oct. de 2019
Respondida: Ajay Pattassery el 29 de Oct. de 2019
Hi I all, I am new to matlab
I am trying to display the result of zm3(result from the high composite-low/2) at each grid point of data of my data set. My dimensions are x=141, y=71 and t=38. The variable rain is in that dimension. When I run the code below on matlab I get the following error message "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 141-by-71". How can I fix this?
for yr=0:38
for j=141
for i=71
zm3(i,j+1)=rain(:,:,2+yr*38);
end
end
end
2 comentariosMostrar NingunoOcultar Ninguno
John D'Errico el 25 de Oct. de 2019
Surely you should have learned by now (having asked 19 questions on Answers), that if you just need to assign a value like 141 to a variable, you do not need to use a for statement? All these lines do is assign the value 141 to j, and 41 to i.
for j=141
for i=71
And worse, because you have created for loops, you then need to have end statements too. Just do this:
j = 141;
i = 41;
desert_scientist90 el 25 de Oct. de 2019
Thanks John for your answer but, that approach still produces the same error message. The issue is that one side is the product of previous calculations zm3 vs the whole data set which is rain. I am trying to represent the value of zm3 on each grid point with data.

Iniciar sesión para comentar.

Respuestas (1)

Ajay Pattassery el 29 de Oct. de 2019
Hello,
The error is generated since the right hand side of
zm3(i,j+1) = rain(:,:,2+yr*38);
is a matrix with dimension 141x71 where the left hand side is just a scalar. Hence assigining a matrix to a scalar position is causing the error.
I assume that you wanted to retrieve rain(:,:,2), rain(:,:,40), rain(:,:,78) etc. till rain(:,:,1446) and assign it to zm3, then you could do the following.
for yr=0:38
zm3(:,:,yr+1)=rain(:,:,2+yr*38);
end
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by