Calculation of summation with filtering positive and negative values
Afficher commentaires plus anciens
Hi
I have one matrix with u(i,j,t).
I need calculate sum of the variable like this in each time step.i,j have not folloed an approprate pattern for example the same i or j
sum(t)= u(167,148,t)+u(167,139,t)+u(167,165,t)+u(167,164,t)+u(167,163,t)+..
But,I need an algorithm to seperate positive and negative values.
for example in each time step check the u: if u is positive: sumpositive(t)=u(167,158,t)+u(167,159,t)+u(167,160,t)+u(167,161,t)+u(167,162,t)
if u is negative : sumnegative(t)=iu(167,158,t)+u(167,159,t)+u(167,160,t)+u(167,161,t)+u(167,162,t)
Thanks
Réponses (1)
% Set seed for reproducibility
rng default
% Some pretend data
u = randn(4,4,3);
% Identify positive u
uIsPos = u >= 0;
% Sum the positive u
sumpositive = sum(u.*uIsPos,3)
% Sum the negative u
sumnegative = sum(u.*~uIsPos,3)
4 commentaires
JAVAD
le 24 Avr 2023
Déplacé(e) : the cyclist
le 24 Avr 2023
the cyclist
le 24 Avr 2023
Modifié(e) : the cyclist
le 24 Avr 2023
Maybe I misinterpreted your question.
My solution sums all elements (separately for positive and negative) along the 3rd dimension, for every (i,j).
The (i,j) element of sumpositive is the sum over k of all positive elements along u(i,j,k).
Looking at the code you just posted, I don't understand exactly which elements you want to sum. But if you define uIsPos the way I did, then you could use
u(200,206,t).*uIsPos(200,206,t)
(etc) to add that element only if it is positive.
It might be helpful for you to upload a very small example (with only a few rows and columns).
JAVAD
le 25 Avr 2023
Déplacé(e) : the cyclist
le 25 Avr 2023
the cyclist
le 25 Avr 2023
for t = 1:length(time)
sumpositive(t) = (u(167,158,t).*(u(167,158,t)>0)) + (u(167,159,t).*(u(167,159,t)>0)) + (u(167,160,t).*(u(167,160,t)>0));
sumnegative(t) = (u(167,158,t).*(u(167,158,t)<0)) + (u(167,159,t).*(u(167,159,t)<0)) + (u(167,160,t).*(u(167,160,t)<0));
end
which can be simplified to
for t = 1:length(time)
sumpositive(t) = sum(u(167,158:160,t).*(u(167,158:160,t)>0));
sumnegative(t) = sum(u(167,158:160,t).*(u(167,158:160,t)<0));
end
Catégories
En savoir plus sur Correlation and Convolution dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!