Effacer les filtres
Effacer les filtres

Counting number of elements less than value across 2d matrix

3 vues (au cours des 30 derniers jours)
John Cruce
John Cruce le 20 Mai 2024
Modifié(e) : Adam Danz le 20 Mai 2024
I have a two-dimensional matrix of numbers size A(720,1440). I'd like to calculate the percent of values in part of the matrix -- i.e., from 401:440 and 1081:1360 -- where values are less than or equal to 5, exclusing NaN values.
In order to accomplish this, I'd like to first count the number of elements for this part of A (401:440,1081:1360) where values are less than 5, but omiting any NaN values.
I'm trying to do this using the sum function like below but I'm striking out. Any suggestions?
sum(A(401:440,1081:1360)<=5)

Réponse acceptée

Adam Danz
Adam Danz le 20 Mai 2024
Modifié(e) : Adam Danz le 20 Mai 2024
> I'd like to calculate the percent of values in part of the matrix -- i.e., from 401:440 and 1081:1360 -- where values are less than or equal to 5, exclusing NaN values.
A = randi(10,720,1440);
mu = mean(A(401:440,1081:1360)<=5,'all','omitnan')
mu = 0.4957
> I'd like to first count the number of elements for this part of A (401:440,1081:1360) where values are less than 5, but omiting any NaN values.
As you can see above, this step isn't needed. But for completeness,
count = sum(A(401:440,1081:1360)<=5,'all','omitnan')
count = 5552
To verity the mean computed above,
totalNumberOfElements = numel(A(401:440,1081:1360))
totalNumberOfElements = 11200
myConfirmed = count/totalNumberOfElements
myConfirmed = 0.4957

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by