Counting repeating element in array

5 views (last 30 days)
Nikita Johnson
Nikita Johnson on 20 May 2017
Answered: Rik on 21 May 2017
I need to solve a small part of my script which I am working on, I need to sort out- how many times the element of an unknown matrix have repeated in the array, but the condition is that I should not use any in-built function of MATLAb, but can use " for, if or while" if required.
Consider a 2D matrix, for example, A=[1,1,2 ;1,2,2;1,3,3], so here 1 is 4 time, 2 is 3 times and 3 is 2 times. so the function which I make should (for example
function similar_no=similar_counter [A]
should give a matrix which contains the elements which tells about the repetition.
One thing to just remember is that the function should be applicable to any 2D matrix and any value of element
kindly help.
  3 Comments
Walter Roberson
Walter Roberson on 20 May 2017
Sorry, that code violates the problem requirements, because it uses the built-in functions colon, subsref(), isequal(), plus(), subsasgn(), and fprintf()
Image Analyst
Image Analyst on 20 May 2017
Will the numbers always be integers from 1 upwards?

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 20 May 2017
That problem is not solvable under the constraints you have been given. See https://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions
  2 Comments
Walter Roberson
Walter Roberson on 20 May 2017
Hint: keep a list of numbers you have seen so far. For each number in the array, compare the new number to each element of the list of numbers; if it shows up on the list, then increment the count that corresponds to that entry; if it does not show up on the list, then add it to the list with count 1.
Jan
Jan on 21 May 2017
@Nikita: If Walter's answer is not clear: Many teachers ask for "solve without built-in function", but this is nonsense. You cannot do anything useful without built-in functions. Even "a=b" calls functions. Nevertheless, we can guess the intention of your teacher. Unfortunately he does exactly the opposite of what is considered to be useful in this forum. The idea of this forum is to share knowledge about using Matlab efficiently, and your teacher asks for using it as few as possible. What a pitty.
It would be a pleasure to suggest unique, histcounts, accumarray or splitapply.

Sign in to comment.


Jan
Jan on 21 May 2017
Start with
function similar_no = similar_counter(A)
The reply will not be unique, because you do not know which element belongs to the value in the output. By the way: "similar_no" is a strange name - "similar" to what? I'd prefer:
function [Element, Number] = similar_counter(A)
Start with creating an array with the maximum number of possible outputs:
Element = nan(1, numel(A));
Number = zeros(1, numel(A));
Use e.g. "doc numel" to find out, what the commands do. Then step through the elements of the Matrix:
for k = 1:numel(A)
...
end
You can check, if the current element A(k) is contained in the array Element already:
index = find(Element == A(k));
For NaNs this comparison is false in every case. If index is empty (see help isempty), the Element did not occur before. Then store it in Element. Therefore you need an index, where the next element has to be inserted. If an occurrence is found, increment the value of Number(index).
Please try to implement this. If you have problems, post the code and ask specific questions.

Rik
Rik on 21 May 2017
If they are all going to be greater or equal to one integer values, you can use them as the indices of a vector.
function [counts,values]=find_similar(A)
output=zeros(1,max(A(:)));%pre-allocate
for idx=1:numel(A)%loop through all positions
output(A(idx))=output(A(idx))+1;%increment count
end
values=find(output);%select only positions with non-zero values
counts=output(values);
end
If you are not allowed to use find, you can generate a new vector 1:numel(A) to generate a values vector. Then you need to loop backward through output, removing the 0-elements and removing the corresponding position in values.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!