Extraction of a sub matrix from a mother matrix
Afficher commentaires plus anciens
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
Plus de réponses (2)
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
le 10 Jan 2018
"How about my answer? Doesn't it look more simpler?"
Birdman
le 10 Jan 2018
But mine is also simpler, right? :) At least no for loops, which is an improvement for me.
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.
Amitrajit Mukherjee
le 10 Jan 2018
Birdman
le 10 Jan 2018
@Stephen: But there is an improvement, right? :)
Amitrajit Mukherjee
le 10 Jan 2018
Modifié(e) : Amitrajit Mukherjee
le 10 Jan 2018
Stephen23
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 = 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
Amitrajit Mukherjee
le 10 Jan 2018
Stephen23
le 10 Jan 2018
@Amitrajit Mukherjee: did you download it, as you were advised to do?
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!