Puting empty value in numeric array

조회 수: 293 (최근 30일)
Ugur Acar
Ugur Acar 2019년 10월 9일
편집: meghannmarie 2019년 10월 9일
Easy question for advanced users, big question for a beginner like me.
I want to put an empty value or no value into a numeric matrix.
As an example;
if true
for n=1:4;
If %some condition is satisfied%
Data(n,1)= % a numeric value like 4%
Else
Data(n,1)= % put nothing in this row %
end
End
end
Something like this,
Data(1,1)=3
Data(2,1)=4
Data(3,1)= no value,it exist but no value in it
Data(4,1)=5

채택된 답변

meghannmarie
meghannmarie 2019년 10월 9일
How about putting a NaN in it?
Data(n,1)= NaN;
  댓글 수: 5
Ugur Acar
Ugur Acar 2019년 10월 9일
Thank you John for 'omitnan'. Can u suggest any solution about calculating the values of NaN elements in the data matrix using interpolate function
Turlough Hughes
Turlough Hughes 2019년 10월 9일
You could sum the data in your array by excluding NaNs during summation:
sum(Data(~isnan(Data)))

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

추가 답변 (1개)

Ugur Acar
Ugur Acar 2019년 10월 9일
you are right, i couldnt put nothing in a array. as i said, in the first step i want to determine empty elements in the array according to my if statement, then i will try to find these values with the help of other known elements using interpolate function.
  댓글 수: 4
meghannmarie
meghannmarie 2019년 10월 9일
편집: meghannmarie 2019년 10월 9일
When you use sum or any other statistics use the nan flag if you do not want those considered:
S = sum(Data, 'omitnan')
if you want to interpolate at the nan values, you can get of an index to all the nan values by using isnan and using interp1:
nan_idx = isnan(Data);
x = 1:numel(Data);
Data(nan_idx) = interp1(x(~nan_idx), Data(~nan_idx),x(nan_idx));
Ugur Acar
Ugur Acar 2019년 10월 9일
Nan flag will solve the problem i believe thanks meghannmarie

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

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by