MATLAB Answers

Puting empty value in numeric array

21 views (last 30 days)
Ugur Acar
Ugur Acar on 9 Oct 2019
Edited: meghannmarie on 9 Oct 2019
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

  0 Comments

Sign in to comment.

Accepted Answer

meghannmarie
meghannmarie on 9 Oct 2019
How about putting a NaN in it?
Data(n,1)= NaN;

  5 Comments

Show 2 older comments
John D'Errico
John D'Errico on 9 Oct 2019
In older versions of MATLAB, you could find tools like nansum, which will compute a sum, while omitting the NaN elements. (nansum is in the stats toolbox.) Hiwever in present day MATLAB, you will find an option to omit the NaNs in sum.
'omitnan' - the sum of a vector containing NaN values
is the sum of all its non-NaN elements. If all
elements are NaN, the result is 0.
However, you CANNOT put an empty value in an array. Arrays do not work like that. Arrays are rectangular things, with SOMETHING in every element. Nothing is not an option.
Ugur Acar
Ugur Acar on 9 Oct 2019
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 on 9 Oct 2019
You could sum the data in your array by excluding NaNs during summation:
sum(Data(~isnan(Data)))

Sign in to comment.

More Answers (1)

Ugur Acar
Ugur Acar on 9 Oct 2019
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 Comments

Show 1 older comment
Ugur Acar
Ugur Acar on 9 Oct 2019
Thank you Steven, i guess what i was looking for is fillmissing function, i will try it
meghannmarie
meghannmarie on 9 Oct 2019
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 on 9 Oct 2019
Nan flag will solve the problem i believe thanks meghannmarie

Sign in to comment.


Translated by