Subscripted assignment dimension mismatch

1 visualización (últimos 30 días)
Kira Bruce
Kira Bruce el 3 de Mzo. de 2020
Editada: per isakson el 5 de Mzo. de 2020
Below is my code. When my program tries to calculate D, it gives me the error as stated in the title. the Varibles N, Eab, den, and V all produce a matrix of 1x47 values. I have tried removing the dot (.) from the equation's multiplication and divisons, but I still get errors (inner matrix dimnesions must agree). Any help is appreciated. Thank you.
row = 0;
col = 0;
for row = 1:1:420
for col = 1:1:420
N = No - No.*exp(-linavg*d);
Eab = E.*(lin./linavg);
D(row,col) = (N.*Eab)./(den.*V);
No = N*exp(-linavg*d);
end
col = col +1;
end
row = row+ 1;
  3 comentarios
Kira Bruce
Kira Bruce el 4 de Mzo. de 2020
Row and col are ment to set the for loop so that it will do the calculation within the for loop for 420 rows and 420 columns, row and col respectively. Col = col + 1 will allow for the for loop for keep going, as will the row version, until it goes through the values I want.
I want D(row,col) to be a vector of 1x47 values. I want the value produced in each for loop to be stored in a matrix so it can be used in other calculations involving the variable D.
Thank you,
Kira
Aquatris
Aquatris el 4 de Mzo. de 2020
Editada: per isakson el 4 de Mzo. de 2020
First of you do not need to specify col = col+1. When you define for col = 1:100, the for loop will increment the col variable every iteration
Secondly, do you want D to be a 3D matrix? If so,
D(row,col,1:47) = (N.*Eab)./(den.*V);

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 5 de Mzo. de 2020
Editada: per isakson el 5 de Mzo. de 2020
See for loop to repeat specified number of times and note especially, "[...]Avoid assigning a value to the index variable within the loop statements. The for statement overrides any changes made to index within the loop." Thus, inside the loop the for-statement totally controls the value of the index.
Try
D = nan( 47, 420, 420 ); % Pre-allocate D before entering the for-loops
for row = 1:1:420
for col = 1:1:420
N = No - No.*exp(-linavg*d);
Eab = E.*(lin./linavg);
D( :, row, col ) = (N.*Eab)./(den.*V);
No = N*exp(-linavg*d);
end
end
By putting the result of the inner loop in the first dimension of D the 47 values are adjacent in memory, which gives a (small) performance gain.

Más respuestas (0)

Categorías

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

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by