Help me with for loop

6 ビュー (過去 30 日間)
Mohammad Alwardat
Mohammad Alwardat 2020 年 11 月 4 日
コメント済み: Mohammad Alwardat 2020 年 11 月 4 日
Hi, I have matrix L with dimension 4680*640 and I need to decompose the matrix into 13 submatrix with dimension 360*640
I wrote matlab code but I got this error
" Index in position 2 exceeds array bounds (must not exceed 640). "
where n1=360 and n2=640.
My code is :
for i = 1:1:n1
for j =1:1:n2
L1(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(1*n2+1):1:(2*n2)
L2(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(2*n2+1):1:(3*n2)
L3(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(4*n2+1):1:(4*n2)
L4(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(5*n2+1):1:(5*n2)
L5(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(6*n2+1):1:(6*n2)
L6(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(7*n2+1):1:(7*n2)
L7(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(8*n2+1):1:(8*n2)
L8(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(9*n2+1):1:(9*n2)
L9(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(10*n2+1):1:(10*n2)
L10(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(11*n2+1):1:(11*n2)
L11(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(12*n2+1):1:(12*n2)
L12(i,j) = L(i,j);
end
end
for i = 1:1:n1
for j =(13*n2+1):1:(13*n2)
L13(i,j) = L(i,j);
end
end

採用された回答

Stephen23
Stephen23 2020 年 11 月 4 日
編集済み: Stephen23 2020 年 11 月 4 日
Rather than writing so much code and using numbered variables (very bad data design), you should just use mat2cell:
L = rand(4680,640); % fake data
idr = 360*ones(1,13);
idc = 640;
out = mat2cell(L,idr,idc);
size(out)
ans = 1×2
13 1
You can access the cell array contents using basic and very efficient indexing:
out{1} % the 1st matrix
out{2} % the 2nd matrix
etc.
  1 件のコメント
Mohammad Alwardat
Mohammad Alwardat 2020 年 11 月 4 日
Thanks, it's work.
I'm a beginner in MATLAB so I always write bad codes :(

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by