Hi, I have a matrix A = [0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 0 0 1]. I want to sum the 1's of its duration so that the result comes as B = [0 0 2 0 0 0 5 0 0 0 0 3 0 0 1]. I need a help in this regard.

 Réponse acceptée

Jan
Jan le 10 Jan 2018
Modifié(e) : Jan le 10 Jan 2018
A = [0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 0 0 1];
[B, N] = RunLength(A);
match = (B == 1);
B(match) = N(match);
N(match) = 1;
Result = RunLength(B, N);
[EDITED] And after a lot of time and struggling a working loop:
R = zeros(1, numel(A) + 1);
iR = 1;
for k = 1:numel(A)
if A(k) == 0
iR = iR + 1 + (R(iR) ~= 0);
else
R(iR) = R(iR) + 1;
end
end
R = R(1:iR - (R(iR) == 0))

6 commentaires

RunLength command is not responding
Birdman
Birdman le 10 Jan 2018
How about my answer? Doesn't it look more simpler?
Hey nice..Yes, this is more easier and working well..Thank you.. :) :)
Jan
Jan le 10 Jan 2018
@Amitrajit Mukherjee: What does "RunLength command is not responding" mean? You have to download it at first from the FileExchange. While RunLength is a fast C-Mex function and has to be compiled at first, RunLength_M is an M-function and can be used directly.
Amitrajit Mukherjee
Amitrajit Mukherjee le 10 Jan 2018
Modifié(e) : Amitrajit Mukherjee le 10 Jan 2018
See, now I actually needed it without downloading any extension..So I would like to prefer the second code which you have suggested..I will also try other codes later..I need a versatile Code which can be used in such computers with no internet..So no option will be there to download any MATLAB function..But other options are also appreciable..Thanks..
Jan
Jan le 11 Jan 2018
@Amitrajit Mukherjee: You can download the files on the computer you use to post in the forum and include the source codes in your program. Copying the code from the forum is not a real difference to copying code from the FileExchange.

Connectez-vous pour commenter.

Plus de réponses (2)

Birdman
Birdman le 10 Jan 2018
Firstly, download the file from the following link and use rcumsum function within it.
Then, apply the following code:
B=rcumsum(A);
idx=setdiff(find(diff(B)==1),find(diff(B)<0)-1)+1;idx(end)=[];
B(idx)=[]

7 commentaires

Stephen23
Stephen23 le 10 Jan 2018
"How about my answer? Doesn't it look more simpler?"
Birdman
Birdman le 10 Jan 2018
But mine is also simpler, right? :) At least no for loops, which is an improvement for me.
Stephen23
Stephen23 le 10 Jan 2018
"But mine is also simpler, right?"
Your answer calls both diff and find twice, setdiff once, requires four lines, and resizes two arrays.
My answer calls diff once, requires two lines, and is simpler.
Sorry rcumsum is not working in this version.. :( :(
Stephen23
Stephen23 le 10 Jan 2018
Modifié(e) : Stephen23 le 10 Jan 2018
@Amitrajit Mukherjee: did you download it, as Birdman clearly told you to do? The function contains only standard MATLAB commands, so there is no reason why it would not work on any version of MATLAB.
Birdman
Birdman le 10 Jan 2018
@Stephen: But there is an improvement, right? :)
Amitrajit Mukherjee
Amitrajit Mukherjee le 10 Jan 2018
Modifié(e) : Amitrajit Mukherjee le 10 Jan 2018
Yah there is...Actually I need a versatile Code which can be used in such computers with no internet..So no option will be there to download any MATLAB function..But other options are also appreciable..

Connectez-vous pour commenter.

Stephen23
Stephen23 le 10 Jan 2018
Download Matt Fig's excellent rcumsum and simply do this:
>> A = [0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 0 0 1];
>> B = rcumsum(A);
>> B = B(B==0 | [diff(B)<0,true])
B =
0 0 2 0 0 0 5 0 0 0 0 3 0 0 1

2 commentaires

rcumsum command is not working
Stephen23
Stephen23 le 10 Jan 2018
@Amitrajit Mukherjee: did you download it, as you were advised to do?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by