# How can I change the Nan values in a matrix by the values in cells array?

1 view (last 30 days)
Hajar Alshaikh on 1 Nov 2022
Commented: Hajar Alshaikh on 1 Nov 2022
f=[1 nan 3 4 5 6;nan 3 nan 5 6 7;3 nan 5 6 nan 8; 4 5 6 7 8 nan;5 6 nan 8 9 0;6 7 8 nan 0 2]
% I want to replace each nan values by one of the following data
data=[8 0 5 9 7 3 6 4]
Input_variables = num2cell(data)
Input_variables{:}
% it works till here. Then, when I want to have the replacment:
f(Input_variables{:})
% I got this error:
Index in position 1 exceeds array bounds. Index must not exceed 6.

VBBV on 1 Nov 2022
f(pt) = cell2mat(data)
Use cell2mat
Hajar Alshaikh on 1 Nov 2022
Thank you so much for your help

Akira Agata on 1 Nov 2022
If you want to replace NaN by the given value in linear index order, the following will be one possible solution:
% Given matrix
f = [...
1 nan 3 4 5 6;...
nan 3 nan 5 6 7;...
3 nan 5 6 nan 8;...
4 5 6 7 8 nan;...
5 6 nan 8 9 0;...
6 7 8 nan 0 2];
% Given data value
data = [8 0 5 9 7 3 6 4];
% Linear index of NaNs in the matrix f
pt = find(isnan(f));
% Replace NaN by data value
f(pt) = data;
% Show the result
disp(f)
1 0 3 4 5 6 8 3 9 5 6 7 3 5 5 6 6 8 4 5 6 7 8 4 5 6 7 8 9 0 6 7 8 3 0 2
##### 2 CommentsShowHide 1 older comment
Hajar Alshaikh on 1 Nov 2022
Also, this error :
Conversion to double from cell is not possible.

### Categories

Find more on Data Type Conversion in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by