I have a problem with indexing in nested for loop.

조회 수: 2 (최근 30일)
Hassen Mohamed Yousif Abdelatif
Hassen Mohamed Yousif Abdelatif 2021년 12월 6일
편집: Walter Roberson 2021년 12월 17일
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