How to find index in cell array to start cumsum

Hello, I have a cell array 'ddvaluecell'.
I would like to find the index of values in these cells which are > 0.5.
Then starts a cumsum for the next 5 indices or stop early when it reaches 3.
How do I do this?

5 Kommentare

indices = cellfun(@(C) find(C>0.5), ddvaluecell, 'uniform', 0);
Nice thank you very much Walter.
Do you know how I can start cumsum for these values?
I would suggest that you write straight forward loops for this task. Get the code working first. Look for opportunities for optimization later once you have working code you can test against.
And remember that vectorizing is not always the best approach. If you are struggling to package the results and to figure out how to feed the data in and out of the vectorized forms then you are losing clarity and running the risk that the convoluted forms are slower than looping.
Also take the time to decide what you want to do if there are multiple nearby values that meet the test. Do you want to process separately for each possible starting point Do you want to bundle all of the values within a window of 5 together only making one start for the window? Even if the sum ends early? Do you want to bundle all consecutive >½ together but only take starting from the first of the group?
Thanks for the suggestion, I'll give it a go

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jonathan Cheong
Jonathan Cheong am 8 Mär. 2021
Bearbeitet: Jonathan Cheong am 8 Mär. 2021

0 Stimmen

Ok, so this is the answer.
% Index of starting cumsum
indices = cellfun(@(C) find(C>0.5), ddvaluecell, 'uniform', 0);
indexes = cellfun(@(x,ii) x(ii),ddindexcell,indices, 'uniform', 0);
% Change cell into array
indexarray = padcat(indexes{:}); % Pad rows with PADCAT function
indexarr = indexarray(:).'; % Change into column form
indexarr = indexarr(~isnan(indexarr)); % Remove all NaN
sumsum = zeros(0,size(rain,2));
indsum = [];
indend = [];
for bi = indexarr % Iterate over start indices (1x140)
cs = cumsum(rain(bi:end));
csp = find(cs <= 2.5); % Stop when reach 2.5mm
if numel(csp) <= 5 % Cumsum less than 5 days is used
inds3_5 = bi-1 + csp;
sumsum = [sumsum;rain(inds3_5,:)]; % append data
indsum = [indsum; inds3_5]; % append indices
ddearly = [indsum, sumsum]; % Index and value side by side
indend = [indend; inds3_5(end)]; % Last index of cumsum
end
end

4 Kommentare

Lol, I accepted my own answer
sum = rain([],:);
That would be empty, in a confusing way. Use zeros(0,size(rain,2))
It also makes sum into a variable which often leads to problems. Use a different variable name.
cs = cumsum(rain(bi:end));
A moment ago you used rain as a 2d array and here as a vector. That is confusing.
Can we assume that rain is Non-negative and never nan?
Yes, rain is never negative.
Good point, I'll change the sum variable to something else.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by