Borrar filtros
Borrar filtros

I have a problem with indexing in nested for loop.

3 visualizaciones (últimos 30 días)
Hassen Mohamed Yousif Abdelatif
Hassen Mohamed Yousif Abdelatif el 6 de Dic. de 2021
Editada: Walter Roberson el 17 de Dic. de 2021
I have a problem with indexing in nested for loop. I am trying to create a matrix where the outer loop for the columns index and the inner loop for the rows index. The problem it is saying ' Index in position 2 exceeds array bounds. Index must not exceed 6' also ' Error in username (line 166)
if femur_6d (rows,columns)~=0' .
Please see the code below :
motion_data = csvread("hip_test_6d.csv",1,1);
hip_6d = motion_data(:,1:6);
femur_6d = motion_data(:,7:12);
for columns=1:size(femur_6d)
for rows=1:size(femur_6d)
if femur_6d (rows,columns)~=0
corrected_femur (rows,columns)=femur_6d (rows,columns);
elseif femur_6d (rows,columns)==0
corrected_femur(rows,columns)=femur_6d (rows-3,columns);
end
end
end

Respuestas (2)

Chunru
Chunru el 6 de Dic. de 2021
for columns=1:size(femur_6d, 2) % number of columns
for rows=1:size(femur_6d, 1) % number of rows

Mathieu NOE
Mathieu NOE el 6 de Dic. de 2021
hello
my suggestion
motion_data = csvread("hip_test_6d.csv",1,1);
hip_6d = motion_data(:,1:6);
femur_6d = motion_data(:,7:12);
%% added %%
[m,n] = size(femur_6d); % rows and columns size
%%%%%%%%%%
for columns=1:n
for rows=1:m
if femur_6d (rows,columns)~=0
corrected_femur (rows,columns)=femur_6d (rows,columns);
elseif femur_6d (rows,columns)==0
corrected_femur(rows,columns)=femur_6d (rows-3,columns);
end
end
end

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by