How to add some symbols into a matrix ?
Show older comments
In fact , I have a Matrix like X =
0 5.2 4.8 28.8
0 0 1.0 33.8
0 0 0 33.4
0 0 0 0
I would like to create like this
0 5.2 4.8 28.8
NaN 0 1.08 33.8
NaN NaN 0 33.4
NaN NaN NaN 0
I wrote something like this, but it does not work very well. because I would like to keep the first column and first row zero value, the second column second row zero value and so on
[m,n]=size (X) for i=1:m; for j=1:n; if X(i,j)==0 X(i,j)= NaN ; end end end
Accepted Answer
More Answers (1)
Andrei Bobrov
on 30 Aug 2011
X(tril(true(size(X)),-1))=nan
add
X(bsxfun(@lt,1:4,(1:4)'))=nan
more
only for cell array
Xc = num2cell(X)
Xc(bsxfun(@lt,1:4,(1:4)'))={'-'}
4 Comments
Niki
on 30 Aug 2011
Walter Roberson
on 30 Aug 2011
text symbols cannot appear in numeric matrices. If you want a number prefixed by "-" then probably the easiest way to do that is to multiply the number by negative 1. But it looks to me like perhaps you want an n-dash rather than a minus sign.
Andrei Bobrov
on 30 Aug 2011
I agree with Walter Robenson
Niki
on 30 Aug 2011
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!