Error: Index in position 2 exceeds array bounds.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I have a loop that loops over the cells in "balls_xyz". If the loop encounters a cell that is empty it is supposed to skip that cell. However, when I run the code:
distances_balls_right = cell(size(balls_xyz)); % preallocate the distances cell array
distances_balls_left = cell(size(balls_xyz)); % preallocate the distances cell array
for i = 1:numel(balls_xyz)
if isempty(balls_xyz(i))
continue; % skip empty cells
end
this_cell = cell2mat(balls_xyz(i));
right_hand = sqrt((this_cell(:,7)-this_cell(:,1)).^2 + (this_cell(:,8)-this_cell(:,2)).^2 + (this_cell(:,9)-this_cell(:,3)).^2);
left_hand = sqrt((this_cell(:,7)-this_cell(:,4)).^2 + (this_cell(:,8)-this_cell(:,5)).^2 + (this_cell(:,9)-this_cell(:,6)).^2);
distances_balls_right{i} = right_hand;
distances_balls_left{i} = left_hand;
end
I get the error:
Index in position 2 exceeds array bounds.
right_hand = sqrt((this_cell(:,7)-this_cell(:,1)).^2 + (this_cell(:,8)-this_cell(:,2)).^2 + (this_cell(:,9)-this_cell(:,3)).^2);
Thanks!
0 comentarios
Respuestas (1)
Cris LaPierre
el 15 de Mayo de 2023
The problem is that your variable this_cell does not appear to have the number of columns that your code expects it to have. So it is not empty, but also not the expected size. The rest of your error message should be telling you the actual number of columns.
% Create a variable with 2 columns
A = rand(2)
% Your error, caused by indexing the 3rd column, which doesn't exist
A(:,3)
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing 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!