Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Please help me in generating multiple matrices and calculation

2 ビュー (過去 30 日間)
MANISH KUMAR
MANISH KUMAR 2016 年 6 月 21 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
this code is for one matrix X, but I want N = 5 such matrices
N = 5; % No. of matrices
P = 5; % The number of rows
T = 10; % The number of columns
% Duration of projects
D = [4; 5; 3; 2; 4];
excludedcount = D-1;
X = zeros(P,T);
for row = 1:P
rv = [1, zeros(1, T - excludedcount(row) - 1)];
X(row, 1 : (T - excludedcount(row))) = rv(randperm(numel(rv)));
end

回答 (1 件)

Jos (10584)
Jos (10584) 2016 年 6 月 21 日
Add an extra loop to create a 3D matrix:
N = 5; % No. of matrices
P = 5; % The number of rows
T = 10; % The number of columns
% Duration of projects
D = [4; 5; 3; 2; 4];
excludedcount = D-1;
X = zeros(P,T);
FinalArray = nan(N,P,T) ; % pre-allocation
for MatrixCount = 1:5
for row = 1:P
rv = [1, zeros(1, T - excludedcount(row) - 1)];
X(row, 1 : (T - excludedcount(row))) = rv(randperm(numel(rv)));
end
FinalArray(MatrixCount,:,:) = X ; % store it
end

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by