I have a problem with indexing in nested for loop.

2 次查看(过去 30 天)
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

回答(2 个)

Chunru
Chunru 2021-12-6
for columns=1:size(femur_6d, 2) % number of columns
for rows=1:size(femur_6d, 1) % number of rows

Mathieu NOE
Mathieu NOE 2021-12-6
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

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by