How to place NaN at diagonal position in cell array?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tha saliem
am 6 Mär. 2018
Kommentiert: Tha saliem
am 7 Mär. 2018
hey all
a={[],-1,-1,0.8,-0.7,[],[]; [],[],0.9,1,[],-0.9,0.6; -1,[],[],0.9,0.2,[],0.8}
how to place diagonal value in each row of 'a'. Diagonal value can be [] or NaN. Like this
out={NaN,[],-1,-1,0.8,-0.7,[],[]; [],NaN,[],0.9,1,[],-0.9,0.6; -1,[],NaN,[],0.9,0.2,[],0.8}
3 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 6 Mär. 2018
Bearbeitet: Andrei Bobrov
am 6 Mär. 2018
a={[],-1,-1,0.8,-0.7,[],[]; [],[],0.9,1,[],-0.9,0.6; -1,[],[],0.9,0.2,[],0.8};
[m,n] = size(a);
z = sort([repmat(1:n,m,1),(1:m)' - .5],2);
t = rem(z.',1) == 0;
out = cell(size(t));
out(t) = a';
out(~t) = {nan};
out = out';
or
a={[],-1,-1,0.8,-0.7,[],[]; [],[],0.9,1,[],-0.9,0.6; -1,[],[],0.9,0.2,[],0.8};
[m,n] = size(a);
out = num2cell(nan(m,n+1));
out(triu(true(m,n+1),1)) = a(triu(true(m,n)));
out(tril(true(m,n+1),-1)) = a(tril(true(m,n),-1));
Weitere Antworten (3)
James Tursa
am 6 Mär. 2018
Assuming there are at least as many columns as rows:
[m,n] = size(a);
out = cell(n+1,m);
x = logical(eye(size(out)));
out(~x) = a';
out(x) = {nan};
out = out';
2 Kommentare
Jos (10584)
am 6 Mär. 2018
a={[],-1,-1,0.8,-0.7,[],[]; [],[],0.9,1,[],-0.9,0.6; -1,[],[],0.9,0.2,[],0.8}
sz = size(a)
out = repmat({NaN}, sz + [0 1])
tf = triu(true(sz))
tfc = false(sz(1),1)
out([tfc tf]) = a(tf)
out([~tf tfc]) = a(~tf)
(btw, why use cell arrays?)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays 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!