Repeating elements in a matrix.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Gratitude Kim
am 27 Jul. 2017
Kommentiert: Gratitude Kim
am 27 Jul. 2017
A=zeros(3,4)
A =
0 0 0 0
0 0 0 0
0 0 0 0
>> [nRows,nCols] = size(A); A(1:(nRows+1):nRows*nCols) = 1
A =
1 0 0 0
0 1 0 0
0 0 1 0
Appreciate your kind assistance on how can I write so that my matrix becomes :
A=[1 1 0 0; 0 1 1 0; 0 0 1 1]
Thanks
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 27 Jul. 2017
Bearbeitet: Andrei Bobrov
am 27 Jul. 2017
A=zeros(3,4);
n = size(A,1);
A([1:n+1:end,n+1:n+1:end]) = 1
or
t = true(3,4);
out = tril(t,1) & triu(t);
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices 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!