A Counter for the Frequency of Occurrence of an Element in a matrix for every iteration

3 次查看(过去 30 天)
I'm trying to make a counter for the amount of times an element has been present in a given matrix per iteration (sort of a loop counter for the individual elements) Since the elements can disappear and new ones may appear, meaning some of the unused counters would have to be reset to save on memory. Ex Iteration 1 x= [1 2 3 4 5 6] counter= [ 1 1 1 1 1 1] Iteration 2 x= [1 2 3 4 5 7] counter= [ 2 2 2 2 2 1] Iteration 3 x= [1 2 3 4 7 8] counter= [ 3 3 3 3 2 1]
Thanks :3
  1 个评论
jgg
jgg 2016-1-19
编辑:jgg 2016-1-19
Are you absolutely sure you need to get rid of some of the counters to save on memory? Unless you have literally millions of possible values, or don't know the possible values ahead of time, it's a lot easier to do this without replacing values.

请先登录,再进行评论。

回答(3 个)

Image Analyst
Image Analyst 2016-1-19
You want a count of the numbers, but only those numbers that appear on the current iteration, not numbers which were on a prior iteration but no longer appear.
First of all, are the numbers all integers? Hopefully because that will make it easier.
What you can do it to use histcounts(). set up your bin edges based on the numbers you expect. Like if the numbers might be integers up to 65535, do
binEdges = 0:1:65535;
Then call histcounts(thisData, binEdges) in your loop. Add the counts there to some master count. Then you'll have to find the bins in your current histogram that are zero (meaning noe of that number during this iteration), and delete them from your master list for display, but not permanently.
This seems like a very weird thing to want to do and I'm not sure you really want to do it. Perhaps you've explained it wrong. But for what it's worth, here is the code:
binEdges = 1:20; % 1 more than your biggest expected number.
allCounts = zeros(1, length(binEdges)-1);
for k = 1 : 4
x = randi(9, 1, 6)
thisHist = histcounts(x, binEdges); % Hist of this set of x values
allCounts = allCounts + thisHist; % Accumulate into master list
% Find numbers in this iteration
nonZeroBins = thisHist > 0;
% Extract from allCounts an array that will eliminate
% bins that were zero in this iteration.
thisCount = allCounts(nonZeroBins)
% Display what bins those actually are below it
theseBins = binEdges(nonZeroBins)
end

jgg
jgg 2016-1-19
编辑:jgg 2016-1-19
Based on what you've outlined, this should work:
cand_values = [1:5];
cand_values = sort(cand_values);
counts = zeros(size(cand_values));
% iteration 1
x1 = [1,2,3,4,5];
%%do this for each loop
in_set = ismember(cand_values,sort(x1))
counts = counts.*in_set + in_set;
%reset the counters for no longer included member, add otherwise
cand_values = sort(x1); %reset the candidate values
% iteration 2
x2 = [1,2,3,4,6]
in_set = ismember(cand_values,sort(x2))
counts = counts.*in_set + in_set;
cand_values = sort(x2); %reset the candidate values
It requires some things to be true about your data, namely that you always get the same number of inputs each iteration; i.e. you don't get 5 one iteration then 7 the next, and that each number occurs at most once per iteration. This agrees with your example; if your real data doesn't you'll have to adapt this slightly.

Star Strider
Star Strider 2016-1-19
I’m not certain how you want to do your iterations, so I won’t attempt to write code to emulate it. This is one relatively simple approach for accumulating them, assuming ‘x’ is always comprised of integers:
counter = zeros(20,1);
x= [1 2 3 4 5 6];
tally = accumarray(x(:), 1);
counter(1:length(tally),:) = counter(1:length(tally)) + tally;
x= [1 2 3 4 5 7];
tally = accumarray(x(:), 1);
counter(1:length(tally),:) = counter(1:length(tally)) + tally;
x= [1 2 3 4 7 8];
tally = accumarray(x(:), 1);
counter(1:length(tally),:) = counter(1:length(tally)) + tally;
ad infinitum ...

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by