# Counting Occurrences in a 2D Array with MATLAB

• MATLAB
• twentyafter4
In summary: MarkerSize',1) %essentially makes a scatter plot that appears % to be 3D due to gangsterness % q(:,1)=x;
twentyafter4
Hey peeps,

I was bored the other day and wanted to paint something, so I started tweaking images in mathematica to make them eaiser to paint/draw and I came across de jong attractors and the following http://flam3.com/flame.pdf" .

Basically I immediately remembered I had forgotten most of my MATLAB but I got reasonably far until hitting a key roadblock, which I think I could have probably overcome with some janky coding but I really want to know the one line method for doing what I really wanted.

As I stated in the title, I basically want to the following as described in the paper:

("Information is lost every time we plot a point that has already been plotted. A more interesting image can be produced if we render a histogram of the chaotic process, that is, increment a counter at each pixel instead of merely plotting. These counters can be visualized by mapping them to shades of gray or by using a color-map that represents different densities (larger count values are more dense) with different colors. A linear mapping of counters to gray values results in Figure 3b.")

Once I had the bin values from said histogram, I could normalize them to highest bin amount and then take the log to give myself a log density map and hence cover a much larger dynamic range. The other things will probably require some more learning but right now this is where I'm stuck.

Heres the simplest way I think I can put forth my issue:

heres my example array where x is column 1 and y is column 2

example=[0 0;
3 3;
1 1;
3 1;
2 2;
2 2;
0 0];

obviously each pair is a data point generated in my code.

I want to get to here:

exfreq=

[ 0 0 2;
1 1 1
2 2 2
3 1 1
3 3 1 ]

Where the third column would be the number of times each 2D point in space got hit. Obviously the repeats in the first two columns would disappear and only unique values would be listed.

I was thinking unique() and accumarray() are probably going to be the answer here but I just don't know how to utilize them and the manual doesn't help. I've been getting incredibly frustrated that I can't seem to find any help what so ever for this type of problem. Simply running sort() on the 2D array puts it in a more useful form where all the x values are grouped but I can't formulate an elegant and quick way to get where I want to be.

Here is the code for two things I did:

dejong attractor:

-----------------------

close all
clear all

i=200000;
x=zeros(1,i);
y=zeros(1,i);

x(1)=3*rand()-1.5; %generates initial points for loop
y(1)=3*rand()-1.5;

A= 4*rand()-2; %generates random coefficients
B= 4*rand()-2;
C= 4*rand()-2;
D= 4*rand()-2;

for n=1:i

x(n+1) = sin(A.*(y(n)))-cos(B.*(x(n))); %eqns for de jong attractor
y(n+1) = sin(C.*(x(n)))-cos(D.*(y(n))); %
end

plot(x,y,'.','MarkerSize',1) %essentially makes a scatter plot that appears
% to be 3D due to gangsterness

q(:,1)=x;
q(:,2)=y;
qr=round(q*1000); % concatenates into one two column vector with integer
% values(perhaps there is a better way to do this histogram.

----------------------------------------

And here is the serpinksi gasket garb:

close all
clear all
clc

i=1000000; % number of iterations

x=zeros(1,i); % preallocates for speed
y=zeros(1,i); % preallocates for speed

x(1)=2*rand()-1; % these values generate a random number within
y(1)=2*rand()-1; % the bicubic square [-1,1]

for n=1:i

z=round(.0+2*rand()); % chaos game, selects btw 0 and 2, converts to integ.

if z == 0

x(n+1) = x(n)/2;
y(n+1) = y(n)/2;

elseif z == 1

x(n+1) = (x(n)+1)/2;
y(n+1) = y(n)/2;

else
x(n+1) = x(n)/2;
y(n+1) = (y(n)+1)/2;
end
end % end chaos game loop

x=x(1,50:end); % dumps first 50 iterations (recommended)
y=y(1,50:end);

plot(x,y,'.','MarkerSize',4) %plots all nice and pretty yo!
axis equal

q(:,1)=x;
q(:,2)=y;
qr=round(q*1000); % concatenates into one two column vector with integer values

example=[0 0;1 1;1 1;1 2;2 2];

Thanks guys,

this is my first post so please don't hesitate to suggest better formatting options for future questions.

Here's an output from the dejong attractor program as of nowhttp://imgur.com/wszvl"

Last edited by a moderator:
Welcome to PhysicsForums!

Your problem is probably that you're trying to work with two separate pieces of information: x and y. So why not just put them together temporarily, for instance z=10x+y (assuming that x and y don't exceed 10)? In your example, you can do this with the following command (the colon notation means every row of column 1 or column 2):
Code:
singleVect=10*example(:,1)+example(:,2);

Now you've got a single vector that you can run unique upon, to produce a vector of unique values. Next, you have to use a for loop to iterate through the unique values and find all occurrences of the unique values. This produces a vector containing the indices of the vector where the value occurs. The product of the size of this vector tells you the frequency.
Code:
uniqueVect=unique(singleVect);
for n=1:prod(size(uniqueVect)
tempVect=find(singleVect==uniqueVect(n));
freqVect(n)=prod(size(tempVect));
end

Separating off the unique x and y values will require you to do the opposite of what you did to put them together in the first place. TIP: there's a modulus function (mod), as well as functions that round (round), round up (ceil), and round down (floor):
http://www.mathworks.com/help/techdoc/ref/mod.html
http://www.mathworks.com/help/techdoc/ref/fix.html

Good luck!EDIT: Oops, it looks like you already know about indexing and the colon operator! Apologies for that! However, for future reference, please put your code between the [ code][/ code] tags without the spaces inbetween the sets of square brackets!

## 1. How can I count the number of occurrences of a specific element in a 2D array using MATLAB?

To count the number of occurrences of a specific element in a 2D array, you can use the built-in function "sum" in MATLAB. This function takes in a logical array as input and returns the sum of all the elements that are true. By converting the 2D array into a logical array where the element you want to count is true and all other elements are false, you can then use the "sum" function to count the occurrences.

## 2. Can I count occurrences of multiple elements in one 2D array using MATLAB?

Yes, you can count occurrences of multiple elements in one 2D array using the same method as mentioned in the previous question. By converting the 2D array into a logical array where each element you want to count is true and all other elements are false, you can then use the "sum" function to count the occurrences of each element separately.

## 3. What if I want to count occurrences of elements within a specific range in a 2D array using MATLAB?

You can use the "find" function in MATLAB to first locate the elements within the desired range in the 2D array. This function returns the indices of the elements that meet the specified condition. You can then use these indices to create a logical array and use the "sum" function to count the occurrences of these elements.

## 4. Is there a faster way to count occurrences in a 2D array using MATLAB?

Yes, you can use the "accumarray" function in MATLAB to count occurrences in a 2D array. This function creates a new array where the values from the original array are grouped together based on their indices. By specifying the function 'sum' as the third input, the "accumarray" function will then sum up the values in each group, resulting in the number of occurrences of each element.

## 5. Can I count occurrences in a 2D array without converting it into a logical array using MATLAB?

Yes, you can use the "histcounts2" function in MATLAB to count occurrences in a 2D array without converting it into a logical array. This function creates a 2D histogram of the data, where each bin represents a range of values. The function then counts the number of elements in each bin, giving you the occurrences of each element in the 2D array.

• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
476
• MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
934
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
449
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K