필터 지우기
필터 지우기

How to place NaN at diagonal position in cell array?

조회 수: 7 (최근 30일)
Tha saliem
Tha saliem 2018년 3월 6일
댓글: Tha saliem 2018년 3월 7일
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
Tha saliem
Tha saliem 2018년 3월 6일
yes a is 3x7 matrix while out will be of 3x8.. one place for diagonal value will be added
Tha saliem
Tha saliem 2018년 3월 7일
Thanks to all for giving useful answers. I am accepting answer based on efficiency of approach as i have to apply it on a large dataset.. Thanks again :)

댓글을 달려면 로그인하십시오.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2018년 3월 6일
편집: Andrei Bobrov 2018년 3월 6일
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));

추가 답변 (3개)

James Tursa
James Tursa 2018년 3월 6일
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
Jos (10584)
Jos (10584) 2018년 3월 6일
nice one, James
Tha saliem
Tha saliem 2018년 3월 7일
A good approach too.. Thank You for your time

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2018년 3월 6일
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
Tha saliem
Tha saliem 2018년 3월 7일
Thank You for your time.. Actually this cell array is a result of some mathematical calculation so i have to use it as a cell array.

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 2018년 3월 6일
Put NaNs on the diagonal:
tf = eye(size(a))==1
a(tf) = {NaN}
  댓글 수: 1
Tha saliem
Tha saliem 2018년 3월 6일
Thankyou for answering but it replaces the diagonal value with NaN. I want to create a new place for Diagonal value and put NaN at that place. Other values remains same

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by