How to place NaN at diagonal position in cell array?
Mostra commenti meno recenti
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 Commenti
Jos (10584)
il 6 Mar 2018
out has different dimensions than a?
Tha saliem
il 6 Mar 2018
Tha saliem
il 7 Mar 2018
Risposta accettata
Più risposte (3)
James Tursa
il 6 Mar 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';
Jos (10584)
il 6 Mar 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?)
1 Commento
Tha saliem
il 7 Mar 2018
Jos (10584)
il 6 Mar 2018
Put NaNs on the diagonal:
tf = eye(size(a))==1
a(tf) = {NaN}
1 Commento
Tha saliem
il 6 Mar 2018
Categorie
Scopri di più su Matrices and Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!