Calculation of summation with filtering positive and negative values

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

답변 (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)
sumpositive = 4×4
0.5377 1.3155 4.0673 1.0193 3.3236 0 3.8041 0 1.4090 2.0875 1.0461 1.6031 3.7177 1.9729 3.3478 0.6277
% Sum the negative u
sumnegative = sum(u.*~uIsPos,3)
sumnegative = 4×4
-1.1930 0 -0.1022 -0.8649 -0.8095 -3.2701 -0.2414 -0.8804 -5.2031 -0.4336 -1.3499 -0.1649 0 -1.7115 -0.3034 -1.3520

댓글 수: 4

JAVAD
JAVAD 2023년 4월 24일
이동: the cyclist 2023년 4월 24일
Thanks for your sugestions.
I amnot sure it works in my case.
I have a matrix with dimention 365*300*1300(time)
I try to calculate it for some grids which are on one line.
The line i or j is not constant. For example following works but cannot seperate u+ and u- seperately.Give me the total
for t= 1:length(time)
sum(t)=u(167,158,t)+u(167,159,t)+u(167,160,t)+u(167,161,t)+u(167,162,t)+u(167,163,t)+u(167,164,t)+u(167,165,t)+u(167,166,t)+u(167,167,t)+u(167,168,t)+u(167,169,t)+u(167,170,t)+u(167,171,t)+u(167,172,t)+...
u(167,173,t)+u(168,174,t)+u(169,175,t)+u(170,176,t)+u(171,177,t)+u(172,178,t)+u(173,179,t)+u(174,180,t)+u(175,181,t)+u(176,182,t)+u(177,183,t)+u(178,184,t)+u(179,185,t)+u(180,186,t)+u(181,187,t)+u(182,188,t)+...
u(183,189,t)+u(184,190,t)+u(185,191,t)+u(186,192,t)+u(187,193,t)+u(188,194,t)+u(189,195,t)+u(190,196,t)+u(191,197,t)+u(192,198,t)+u(193,199,t)+u(194,200,t)+u(195,201,t)+u(196,202,t)+u(197,203,t)+u(198,204,t)+...
+u(199,205,t)+u(200,206,t)+u(201,207,t)+u(202,208,t)+u(203,209,t)+u(204,210,t)+u(205,211,t)+u(206,212,t)+u(207,213,t)+u(208,214,t)
end
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
JAVAD 2023년 4월 25일
이동: the cyclist 2023년 4월 25일
I try to sum in each time step.
Here u(i,j,t); t is time
what I need is that;
for t= 1:length(time)
sum(t)=u(167,158,t)+u(167,159,t)+u(167,160,t)
end
it works but it cannot seperate positive and negative values.it sums u which can be positive and negative
I need one algorithm
if u>0 ; sum all u+
if u<0; sum all u-
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

댓글을 달려면 로그인하십시오.

카테고리

질문:

2023년 4월 24일

댓글:

2023년 4월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by