I need to create a matrix with for loop.

1 回表示 (過去 30 日間)
sermet
sermet 2014 年 11 月 27 日
回答済み: Govind Narayan Sahu 2018 年 7 月 6 日
x=[100;120;130;140;150];
y=[120;130;140;150;170];
z=[130;150;170;180;190];
%I need to create this matrix with loop because the number of the rows can be changed;
A=[x(1) y(1) z(1) 0 0 0 0 0 0;0 0 0 x(1) y(1) z(1) 0 0 0;0 0 0 0 0 0 x(1) y(1) z(1);x(2) y(2) z(2) 0 0 0 0 0 0;0 0 0 x(2) y(2) z(2) 0 0 0;...
0 0 0 0 0 0 x(2) y(2) z(2);x(3) y(3) z(3) 0 0 0 0 0 0;0 0 0 x(3) y(3) z(3) 0 0 0;0 0 0 0 0 0 x(3) y(3) z(3);x(4) y(4) z(4) 0 0 0 0 0 0;...
0 0 0 x(4) y(4) z(4) 0 0 0;0 0 0 0 0 0 x(4) y(4) z(4);x(5) y(5) z(5) 0 0 0 0 0 0;0 0 0 x(5) y(5) z(5) 0 0 0;0 0 0 0 0 0 x(5) y(5) z(5)]
thanks in advance.

採用された回答

Guillaume
Guillaume 2014 年 11 月 27 日
xyz = [x y z];
A = cellfun(@(row) blkdiag(row, row, row), num2cell(xyz, 2), 'UniformOutput', false);
A = vertcat(A{:})
You can always replace the cellfun by a for loop if you really want it.

その他の回答 (1 件)

Govind Narayan Sahu
Govind Narayan Sahu 2018 年 7 月 6 日
clear;clc
n =1:1:3;
for i = 1:length(n)
A{i} = [n(i) 0;
0 2*n(i)]
AA = blkdiag(A{:})
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by