Filter löschen
Filter löschen

How to find the number of continuous data set along each row in the matrix given below? Desired result given below.

2 Ansichten (letzte 30 Tage)
V =[0 0 1 1 1 0 1 1;1 1 0 0 0 0 1 1;0 0 1 1 1 0 0 0;1 1 1 0 0 1 1 1;0 1 1 1 0 0 1 1;0 1 1 0 0 1 1 0;0 1 1 1 1 1 1 0]
desired_result=[2;2;1;2;2;2;1]
  3 Kommentare
Payel
Payel am 1 Jul. 2023
By continuous data I tried to mean consecutive non-zero values. In the first row there are 2 sets of continuous data; first set (1 1 1) and second set (1 1). In the third row there is only one such set i.e. (1 1 1). I want to calculate the number of sets of such continuous data along each row. Desired result variable shows the number of such sets along each row.
John D'Errico
John D'Errico am 1 Jul. 2023
Exactly what is different in this question from the one where you got two answers already?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Animesh
Animesh am 1 Jul. 2023
Hey @Payel
You can try something like this :
V = [0 0 0 0 0 0 0 0;
1 1 0 0 0 0 1 1;
0 0 1 1 1 0 0 0;
1 1 1 0 0 1 1 1;
0 1 1 1 0 0 1 1;
0 1 1 1 1 1 1 0;
0 1 1 1 1 1 1 0];
desired_result = zeros(size(V, 1), 1);
for i = 1:size(V, 1)
counter = 0;
for j = 1:size(V, 2)
if (V(i, j) ~= 0) && (j == 1 || V(i, j - 1) == 0)
counter = counter + 1;
end
end
desired_result(i) = counter;
end
desired_result

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by