How to get the sum of numbers below a desired number in sets?

3 次查看(过去 30 天)
How to get the sum of the numbers in the array which are below -1 in sets.
For example
a=[1 -2 0 .5 -3 -4 5 7 -2 -3 -4];
so here the numbers which are consecutively less than -1 should be added only.
i.e. The answer should be
b=[-2 -7 -10]
PS: Actual data is very big.

采纳的回答

Image Analyst
Image Analyst 2018-12-8
编辑:Image Analyst 2018-12-8
These two lines of code will do it.
a = [1 -2 0 .5 -3 -4 5 7 -2 -3 -4];
props = regionprops(a < -1, a, 'Area', 'MeanIntensity');
output = [props.Area] .* [props.MeanIntensity]
How big are your arrays? This should be pretty speedy for up to tens or hundreds of millions of elements. If you have gigabyte sized arrays, it could take a few minutes. The code requires the Image Processing Toolbox.

更多回答(1 个)

per isakson
per isakson 2018-12-8
编辑:per isakson 2018-12-9
"data is very big" How big is that? Does it fit in the memory?
Test this function
function b = cssm( a )
%{
b = cssm([-2,0,.5,-3,-4,5,7,-2,-3,-4]);
b = cssm([1,-2,0,.5,-3,-4,5,7,-2,-3,-4]);
b = cssm([1,-2,0,.5,-3,-4,5,7,-2,-3,-4,3]);
a = randn(1,1e6);
tic, b = cssm( a ); toc
%}
%%
b = zeros(size(a));
%%
kk = 1;
for jj = find(a<-1,1,'first') : length(a)
if a(jj) < -1
b(kk) = b(kk) + a(jj);
else
if b(kk) ~= 0
kk = kk+1;
end
end
end
if a(end) < -1
b( kk+1 : end ) = [];
else
b( kk : end ) = [];
end
end
Performance comparison with the solution of Image Analyst
The test below indicates that the solution using a for-loop is significantly faster - in this case.
>> a = randn(1,1e6);
>> [ isEQ, et ] = cssm( a )
isEQ =
logical
1
et =
0.0069 1.0598
>> a = randi([-12,12],1,1e6,'int8');
>> [ isEQ, et ] = cssm( a )
isEQ =
logical
1
et =
0.0138 3.5893
where
function [ isEQ, et ] = cssm( a )
tic
b1 = loop( a );
et(1) = toc;
tic
b2 = ia( a );
et(2) = toc;
isEQ = all(abs(b1-b2)<1e-6);
end
function b = loop( a )
%%
b = zeros(size(a));
%%
kk = 1;
for jj = find(a<-1,1,'first') : length(a)
if a(jj) < -1
b(kk) = b(kk) + a(jj);
else
if b(kk) ~= 0
kk = kk+1;
end
end
end
if a(end) < -1
b( kk+1 : end ) = [];
else
b( kk : end ) = [];
end
end
function output = ia( a )
props = regionprops(a < -1, a, 'Area', 'MeanIntensity');
output = [props.Area] .* [props.MeanIntensity];
end
Caveat: It happens that I maketakes.

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by