Filter löschen
Filter löschen

How do I keep consecutive number in a matrix?

2 Ansichten (letzte 30 Tage)
Linjun He
Linjun He am 25 Dez. 2018
Kommentiert: Linjun He am 25 Dez. 2018
For a matrix A,
A = [1 1 0 0 1 1 0 0;
1 0 0 0 1 1 0 0;
0 1 0 0 1 1 0 0]
I am trying to get B. How do I make it?
B = [1 1 0 0 0 0 0 0;
1 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0]
If the 1st number of a row is 1,
then the 1st sequence of consecutive 1 is kept and the rest numbers in that row turn 0.
i.e., A(1,:) -> B(1,:), A(2,:) -> B(2,:)
If the 1st number of a row is 0,
then all numbers in that row turn 0.
i.e., A(3,:) -> B(3,:)

Akzeptierte Antwort

Matt J
Matt J am 25 Dez. 2018
B=cummin(A,2);
  2 Kommentare
Image Analyst
Image Analyst am 25 Dez. 2018
Clever. I didn't know about this function. +1 vote.
Linjun He
Linjun He am 25 Dez. 2018
Cool!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by