In a logical array find how the density of TRUE values increases

조회 수: 2 (최근 30일)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013년 8월 6일
편집: Spencer Talbot 2023년 2월 23일
Supposedly we have a logical array:
t=[1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0];
The true values at the beginning are very few but the true values become more and more at the end of the array. Is there any way to identify the increasing density of the true values?
  댓글 수: 6
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 7일
편집: Azzi Abdelmalek 2013년 8월 7일
Giorgos, please, do not answer your question when you want to add a comment. Click on comment on this answer if you want to add a comment to any answer

댓글을 달려면 로그인하십시오.

답변 (4개)

Image Analyst
Image Analyst 2013년 8월 6일
You have to define density over some area (number of elements) because, obviously, the density changes depending on how many elements it's calculated over. For example, to calculate the density over a window size of 5, try this code:
t=[1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0];
density = conv(t, ones(1, 5)/5, 'valid')
plot(density, 'b-', 'LineWidth', 3);
grid on;
xlabel('Element', 'FontSize', 30);
ylabel('Density', 'FontSize', 30);
conv() is a function that will give the sum in the window and then divide by 5 so it gives the average value within the window of length 5.
  댓글 수: 3
Image Analyst
Image Analyst 2013년 8월 6일
You can do that if you want.
% densityThreshold is some value you define.
aboveDensityThreshold = density > densityThreshold;
% Assign original data to false in those locations.
t(aboveDensityThreshold) = false;
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013년 8월 10일
Thank you Image Analyst for your suggestion but I haven't solved yet my original problem neither with the "density" nor with the cumsum. I posted in the beginning of this thread my original question.
I would appreciate if you could take a look at it. And if there is something unclear I would be happy to explain it further.

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 6일
What you did in your previous comment is
sort(t)
I do not think this is what you want. Can you explain mathematically what is density?
  댓글 수: 4
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 6일
편집: Azzi Abdelmalek 2013년 8월 6일
Edit
t=[1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 ];
s=num2str(t)
s1=strrep(s,' ','')
n1=cellfun(@numel,regexp(s1,'1+','match'))
n2=cellfun(@numel,regexp(s1,'0+','match'))
stairs(cumsum(n1))
hold all
stairs(cumsum(n2))

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 6일
편집: Azzi Abdelmalek 2013년 8월 6일
%or maybe
t=[1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 ];
s=num2str(t)
s1=strrep(s,' ','')
n1=cellfun(@numel,regexp(s1,'1+','match'))
n2=-cellfun(@numel,regexp(s1,'0+','match'))
out=zeros(1,numel([n1 n2]))
out(1:2:2*numel(n1)-1)=n1
out(2:2:2*numel(n2))=n2
stairs(cumsum(out))
%or maybe
close
t=[1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 ];
t(t==0)=-1
stairs(cumsum(t))

Spencer Talbot
Spencer Talbot 2023년 2월 23일
편집: Spencer Talbot 2023년 2월 23일
I know this question is old, but I recently had a similar question. Here's what I came up with:
t=[1 0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0];
density = zeros(length(t),2); % initialize the density matrix (1st column is # of consecutive TRUE instances, 2nd column is the index where that streak occurs)
k = 1; % initialize the index for keeping track of density
for i = 1:length(t)
if t(i)
density(k) = density(k,1) + 1; % if the current value is true, plus one count for density
if density(k,1) == 1
density(k,2) = i; % If it's the start of a new streak, record the index
end
else
k = k + 1; % if a streak ends, move on to the next index to prepare for a new streak
end
end
[max_density, idx] = max(density(:,1)); % find the value and starting index for the longest streak
range_idx = density(idx,2):density(idx,2) + max_density - 1; % compute the range of indices for which the max density streak lasts

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by