ignore NaN value in the matris
Mostra commenti meno recenti
I want to ignore NaN values in my matrix. But I don't want it to sum or average the matrix. I just want it to operate with existing values, ignoring values that NaN in the matrix. Can you help me?
7 Commenti
Star Strider
il 3 Nov 2023
That depends on what you want to do. The ‘omitnan’ or 'omitmissing' flags simply delete the NaN values before performing the calculation. If you are doing row-wise or column-wise calculations, that would likely be your best option as well, the doing the operation on each row or column in a loop.
If you are doing matrix calculations, removing the NaN values destroys the matrix architecture, so that is not an option. The best approach in that instance might be to interpolate the missing values. The interpolation method depends on where the NaN values are in the matrix (if they are at the row or column ends, 'nearest' would liely be the best option), and what you want to do with them.
Dyuman Joshi
il 3 Nov 2023
Modificato: Dyuman Joshi
il 3 Nov 2023
As mentioned earlier, what is the operation that you want to do/perform?
Ali Topal
il 3 Nov 2023
Dyuman Joshi
il 3 Nov 2023
Spostato: Dyuman Joshi
il 16 Dic 2023
You can change the NaN values to 0 -
%let mat be your matrix
idx = isnan(mat);
mat(idx) = 0;
%or combined
mat(isnan(mat)) = 0;
Star Strider
il 3 Nov 2023
On the basis of what you have already written here, what you want to do is likely impossible, given the constraints you are imposing.
Dyuman Joshi
il 3 Nov 2023
Modificato: Dyuman Joshi
il 7 Nov 2023
@Ali Topal, What error(s) do you get with NaN values?
Rik
il 18 Dic 2023
If you explain what exactly you want to do, we might be able to suggest a solution. The constraints you describe make a solution impossible, so any solution will require breaking one of your requirements (but that might not actually be a problem).
Risposte (1)
There are a few ways to do this. Let's make an example vector with NaNs
x = rand(1,10);
x(1:4:end) = NaN
Now, for SUM, you probably just want to omit the NaNs using logical indexing.
x(~isnan(x)) % Returns only the non-NaN values
sum(x(~isnan(x)))
sum(x,"omitnan")
Not every function has an "omitnan" flag, so the first example would work when that's not the case.
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!