I have a problem with indexing in nested for loop.

3 ビュー (過去 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
  2 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 12 月 17 日
hello
problem solved?
Hassen Mohamed Yousif Abdelatif
Hassen Mohamed Yousif Abdelatif 2021 年 12 月 17 日
hi @Mathieu NOE , yes thank you .

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by