I have a problem regarding an Index because it exceeds array bounds.

1 visualización (últimos 30 días)
Daniela Vallejo
Daniela Vallejo el 19 de Abr. de 2023
Editada: Cris LaPierre el 19 de Abr. de 2023
I have the following lines of code in a funtion EnergyCurves:
dX=cell(size(p1,1),1);
for i=2:size(p1,1)
dX(i,1)={X(i,:)-X(i-1,:)};
end
The size of p1 is 600001 x 37
The size of X is 600001 x 37
and when I am running the following lines of code calling that function, I obtaing the following error
Xsc=load('dispsc.mat'); % Load the file as a structure array
Xprisc=load('velsc.mat');
% Convertir la estructura de celdas 'X' en un arreglo de celdas en MATLAB
Xsc=struct2cell(Xsc);
% Convertir la estructura de celdas 'Xpri' en un arreglo de celdas en MATLAB
Xprisc=struct2cell(Xprisc);
% Utiliza la función '{:}' para acceder al contenido de cada celda en el arreglo 'X' y 'Xpri' y concatenarlos en un solo arreglo
Xsc=[Xsc{:}]';
Xprisc=[Xprisc{:}]';
[Eisc, Essc, Edsc, Eksc] = EnergyCurves(M,C,K,z, Mt, pisos, Xsc, Xprisc,E)
Error:
Index in position 1 exceeds array bounds. Index must not exceed 37.
Error in EnergyCurves (line 32)
dX(i,1)={X(i,:)-X(i-1,:)};
Error in GraficasEnergiaViento (line 57)
[Eisc, Essc, Edsc, Eksc] = EnergyCurves(M,C,K,z, Mt, pisos, Xsc, Xprisc,E)

Respuestas (2)

Cris LaPierre
Cris LaPierre el 19 de Abr. de 2023
Editada: Cris LaPierre el 19 de Abr. de 2023
I suspect X is not the size you think it is. Since your error is saying that the max number of rows is 37, it appears that perhaps X has been transposed? However, it is impossible for us to know, as the code that creates X is inside your function, and you haven't shared that code with us.

dpb
dpb el 19 de Abr. de 2023
Movida: dpb el 19 de Abr. de 2023
It would appear that the X array is 37xN instead of Nx37...
BTW, you don't need a loop to compute the difference; diff() is vectorized
dX=diff(X.');
instead, if you want to treat X as long-way vertical instead; transpose it first before doing the difference.
Alternatively,
dX=diff(X,1,2).';
does the same thing except takes the first difference along the second dimension (between columns) and then transposes the result to again be longer than wide.
There's no advantage in using a cell array here, either, just keep with the native double array.
To prove is the correct interpretation/assumption, show us what
size(X)
returns; that's the piece of information we're missing that is crucial

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by