The simple case is like this:
2 1 4 6 2
9 4 6 1 2
5 3 2 8 3
7 2 1 9 3
7 1 8 2 4
From the matrix above, i want to insert 3 NaNs in random place. So, my code is like this:
Data = [2,1,4,6,2;9,4,6,1,2;5,3,2,8,3;7,2,1,9,3;7,1,8,2,4];
[rows,cols] = size(Data);
p = 3;
r = randperm(25);
r = r(1:3);
i = 1;a = 1; b = 1;
while i <= 3
n = r(a,b);
b = b+1;
e = 1;
if n <= cols
Data(1,n) = NaN;
else
if n > cols
while n > cols
e = e+1;
k = n - cols;
n = k;
end
Data(e,n) = NaN;
end
end
i = i+1;
end
The output one of the output will be like this:
2 1 4 6 2
9 NaN NaN NaN 2
5 3 2 8 3
7 2 1 9 3
7 1 8 2 4
So, i want to make some constraint such as:
1. every row only can have 2 NaN
2. amount NaN in column 1 have to be less then column 2, and amount NaN in column 2 have to be less then column 3, and so on. eg. output matrix will be like this:
2 1 4 6 2
9 4 6 1 NaN
5 3 2 8 3
7 2 1 NaN 3
7 1 8 2 NaN
for matrix above we can see that:
amount NaN of column 1= 0, column 2=0, column 3=0, column 4=1, column 5= 2.
Somebody can help me to insert those my constraint into my code above? Or there willl be another solution i think.
Thanks before :')
2 Comments
per isakson (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/36154-how-to-replace-some-of-the-value-in-the-matrix-with-nan#comment_74987
Isti (view profile)
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/36154-how-to-replace-some-of-the-value-in-the-matrix-with-nan#comment_74996
Sign in to comment.