How can I quantify the number of cells between specific values?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a 100x1500 array [A] filled with zeros and ones.
For example, A(1,:) = 0 0 0 0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1....
For each row, I would like to find the average distance, or average number of zeros, between ones. Is there a simple way to do this?
3 Kommentare
Voss
am 9 Dez. 2022
Bearbeitet: Voss
am 9 Dez. 2022
@omidm: How should consecutive ones be treated? That is, should they be considered as having a run of zero zeros in between them? Consider:
A = [1 0 1 1 0 0 0 1]
Is that a sequence of 1 zero followed by a sequence of 3 zeros (average length 2), or is it a sequence of 1 zero followed by a sequence of 0 zeros followed by a sequence of 3 zeros (average length 4/3)?
Akzeptierte Antwort
Voss
am 9 Dez. 2022
A = [0 0 1 0 0 0 0 1 1 0
1 0 1 0 0 0 1 1 0 0
0 1 1 1 0 0 1 0 1 0
1 0 0 0 1 1 1 0 1 0
1 1 1 0 1 0 0 0 0 1
1 0 0 1 1 1 1 1 1 0
0 0 1 0 1 0 0 0 0 0
1 1 1 1 0 1 0 1 0 1
0 0 0 0 1 1 0 1 0 1
0 1 0 1 0 1 1 1 1 0];
[r,c] = find(A);
groupsummary(c,r,@mean_diff)
function out = mean_diff(x)
m = diff(x)-1;
out = mean(m(m > 0));
end
Weitere Antworten (1)
Eric Delgado
am 9 Dez. 2022
I don't know if my solution is the "simple way" that you request! :)
randomMatrix = randi([0,1],10)
meanZeros = zeros(height(randomMatrix), 1);
for ii = 1:height(randomMatrix)
meanZeros(ii) = mean(diff(find(randomMatrix(ii,:) == 1))-1);
end
meanZeros
1 Kommentar
Voss
am 9 Dez. 2022
Note that this method treats consecutive 1's as containing zero 0's in between them, e.g., for the first row of A above, this method gives the average number of zeros as 2 = (4+0)/2 rather than 4 = 4/1.
It's not clear whether this is what OP intends.
Siehe auch
Kategorien
Mehr zu Logical 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!