Nested For Loop Value Appended to Overall Matrix

조회 수: 1 (최근 30일)
John O'Donnell
John O'Donnell 2021년 3월 4일
편집: KSSV 2021년 3월 4일
Hello, very new to Matlab and wrote this loop to create a state space for a 1820x5 matrix, it'll give me all of the rows, but then I need those values to be in a single matrix
code is as follows:
lim = 12;
S = [];
for a = 0:lim
for b = 0:(lim - a)
for c = 0:(lim - a - b)
for d = 0:(lim - a - b -c)
e = lim - a - b - c - d
new_row = [a b c d e]
end
end
end
end

답변 (1개)

KSSV
KSSV 2021년 3월 4일
lim = 12;
count = 0 ;
A = zeros([],5) ;
for a = 0:lim
for b = 0:(lim - a)
for c = 0:(lim - a - b)
for d = 0:(lim - a - b -c)
count = count+1 ;
e = lim - a - b - c - d ;
new_row = [a b c d e] ;
A(count,:) = new_row ;
end
end
end
end

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by