フィルターのクリア

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

2 ビュー (過去 30 日間)
Hajar Alshaikh
Hajar Alshaikh 2022 年 11 月 1 日
コメント済み: Hajar Alshaikh 2022 年 11 月 1 日
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
VBBV 2022 年 11 月 1 日
f(pt) = cell2mat(data)
Use cell2mat
  6 件のコメント
VBBV
VBBV 2022 年 11 月 1 日
編集済み: VBBV 2022 年 11 月 1 日
f(pt) = cell2mat(double(var))
Ok. That's because the above input is incorrect since the cell2mat takes cell arrays as input and converts to numeric array . But double(var) is not a cell array data type. It's already numeric,hence it's shows error when it encounters incorrect brace such as ( or [ Cell arrays begin with '{'. e.g. in this line
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};
Hope this clears your doubt
Hajar Alshaikh
Hajar Alshaikh 2022 年 11 月 1 日
Thank you so much for your help

サインインしてコメントする。

その他の回答 (1 件)

Akira Agata
Akira Agata 2022 年 11 月 1 日
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 件のコメント
Hajar Alshaikh
Hajar Alshaikh 2022 年 11 月 1 日
I got this error:
Error using indexing
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
I will copy the code from the begining:
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]
numnan=8 % number of nan in a matrix
for s =1:numnan
var(s) =min(min(f,[],'omitnan'))+(max(max(f,[],'omitnan'))-min(min(f,[],'omitnan')))*rand();
end
double(var)
data= num2cell(double(var))
pt = find(isnan(f))
% Replace NaN by data value
f(pt) = data
Hajar Alshaikh
Hajar Alshaikh 2022 年 11 月 1 日
Also, this error :
Conversion to double from cell is not possible.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by