Filter löschen
Filter löschen

help for matrix manipulation with loop

1 Ansicht (letzte 30 Tage)
sermet
sermet am 4 Jun. 2015
Kommentiert: Stephen23 am 4 Jun. 2015
%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 Kommentar
Stephen23
Stephen23 am 4 Jun. 2015
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...

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 4 Jun. 2015
[zeros(3);eye(3)]

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 4 Jun. 2015
Bearbeitet: Andrei Bobrov am 4 Jun. 2015
n= 3;
out = [zeros(n);eye(n)];
with loop
out = zeros(2*n,n);
for jj = 1:n
out(jj+3,jj) = 1;
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by