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

 Akzeptierte Antwort

meghannmarie
meghannmarie am 9 Okt. 2019

3 Stimmen

How about putting a NaN in it?
Data(n,1)= NaN;

5 Kommentare

Ugur Acar
Ugur Acar am 9 Okt. 2019
Bearbeitet: Ugur Acar am 9 Okt. 2019
Thank you for ur answer.i tried it.
it puts "Nan" as u said.
but after for loop, i need sum of all values in data matrix,
sum_of_data=sum(data)
but this time result is NaN. not a numerical value, although there are some numerical values in it, it doesnt sum, instead it gives 'Nan' instead of a value whenever a row exist with 'NaN' in it..
Actually, if i can put an empty value in some rows in the first step, i will try to find these empty values using interpolate function in the second step.
John D'Errico
John D'Errico am 9 Okt. 2019
Exactly. You cannot put nothing in an array element. You can only assign it a specific value, perhaps 0, or better yet, NaN or inf. NaNs are the customary way to treat data as missing in MATLAB.
Then you can use tools that will do computations while ignoring NaN elements, plot does not plot NaNs. And you can locate NaNs using isnan (but never test for something being equal to NaN.)
John D'Errico
John D'Errico am 9 Okt. 2019
Bearbeitet: John D'Errico am 9 Okt. 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 am 9 Okt. 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
You could sum the data in your array by excluding NaNs during summation:
sum(Data(~isnan(Data)))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Ugur Acar
Ugur Acar am 9 Okt. 2019

0 Stimmen

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 Kommentare

Steven Lord
Steven Lord am 9 Okt. 2019
If you put NaN values in your array, you can try to fill them in later using the fillmissing function.
Ugur Acar
Ugur Acar am 9 Okt. 2019
Thank you Steven, i guess what i was looking for is fillmissing function, i will try it
meghannmarie
meghannmarie am 9 Okt. 2019
Bearbeitet: meghannmarie am 9 Okt. 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 am 9 Okt. 2019
Nan flag will solve the problem i believe thanks meghannmarie

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by