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

1 vue (au cours des 30 derniers jours)
Daniela Vallejo le 19 Avr 2023
Modifié(e) : Cris LaPierre le 19 Avr 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
% 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)
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (2)

Cris LaPierre le 19 Avr 2023
Modifié(e) : Cris LaPierre le 19 Avr 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.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

dpb le 19 Avr 2023
Déplacé(e) : dpb le 19 Avr 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
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by