Cumsum at different starting indices, append all previous value.

1 次查看(过去 30 天)
Hello, say I have 2 continuous dataset for example:
A = [0 0 8 0] B = [1 0 0 0 0.5 1.5 0]
The algorithm iterates over each element i = i +1 to check if element >= 0.5. If True cumsum starts.
Condition: Cumsum ends when exceeds 2 within 3 indices. Append the values and index.
The final cumsum value is not important, it is a means to set the condition of appending relevant values.
So result for A will give:
result = [0, 0, 8], idx = 1, 2, 3 NOT result = [ 0 0 8 '0']
However, if the condition is not met. Ignore the cumsum and move on to the next value.
e.g.: B will start 1st cumsum at [1]: 1 + 0 + 0 = 1. Which fails to meet the condition. Hence ignored.
It continues iterating, until reaching 0.5 which then starts another cumsum.
So result for B will give:
val = [1 0 0 0 0.5 1.5] NOT [1 0 0] [1 0 0 0 0.5 1.5 '0']
How can I approach this? Many thanks.
  3 个评论
Jonathan Cheong
Jonathan Cheong 2021-3-3
编辑:Jonathan Cheong 2021-3-3
Sure let me try to elaborate,
Say we iterate over each element: i = i + 1 to check if the element is >= 0.5
For A: Because both zero [0] [0] at index 1 and 2 is less than 0.5 the cumsum doesnt happen.
When it reaches element [8], which is greater than 0.5, it starts a cumsum.
8 + end because the cumsum >= 2, within 3 indices.
It appends all the previous value and the cumsum final value to storage.
Result = [0 0 8] NOT [0 0 8 '0']
For B: 1st element [1] is greater than 0.5 hence cumsum starts:
1 + 0 + 0 = 1. Because the sum does not exceed 2 and is within 3 indices, this cumsum is ignored.
Then the algorithm continutes to iterate the data, and starts cumsum at [0.5] because >= 0.5
[0.5] + [1.5] + end. Because the cumsum >= 2 within 3 indices.
Then, it appends all previous value until final cumsum value to storage. It doesn't include the final 0.
Result = [1 0 0 0 0.5 1.5] NOT [1 0 0] or [1 0 0 0 0.5 1.5 '0']
Note: The final cumsum value doesn't matter. It is just a means to set a condition so that correct data is appended.
Many thanks in advance
Jonathan Cheong
Jonathan Cheong 2021-3-3
Here is my code, it doesn't work but hopefully this will help.
a = 0;
dd = [];
ind = [];
for row = idx
a = a + 1; % Iterate over each indices
if rain(idx(a)) >= 0.5 % If value >= 0.5 start cumsum
cs = cumsum(rain(idx(a):end);
cmp = find(cs <= 2); % Find index where cumsum ends
if numel(cmp) > 3
continue % If index >3, ignore this interval
end
% Append all relevant vlaues?
ind2 = a + cmp;
ind = [ind; ind2];
end
end
The files are also attached below.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by