help for matrix manipulation with loop

2 次查看(过去 30 天)
%for n=i (i is variable, taken 6 this example), I need to create the below matrix % i numbers rows and (i-3) numbers columns need to be created as follow;
j=[0;0;0;1;0;0]
m=[0;0;0;0;1;0]
k=[0;0;0;0;0;1]
created_matrix=[j,m,k]
%I need to write a loop w.r.t. the i variable for creating this matrix.
  1 个评论
Stephen23
Stephen23 2015-6-4
Using vectorized code is much neater, faster and more robust than using loops. Learn to write vectorized code and suddenly MATLAB becomes a powerful tool for you to use...

请先登录,再进行评论。

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2015-6-4
[zeros(3);eye(3)]

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2015-6-4
编辑:Andrei Bobrov 2015-6-4
n= 3;
out = [zeros(n);eye(n)];
with loop
out = zeros(2*n,n);
for jj = 1:n
out(jj+3,jj) = 1;
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by