Finding the frequency of each number in a string

Hi, I have a string of numbers like s=[(11,23),(33,47),(98,20),...,(34,65)] The biggest number is 100. How can I order the numbers s based on their frequency of repetition in s? For example for a simple case like s1=[(1,4),(2,4),(4,1)] the result is
  • Number 4 frequency 3
  • Number 1 frequency 2
  • number 2 frequency 1

6 commentaires

This is not a valid Matlab syntax:
s = [(11,23),(33,47),(98,20),...,(34,65)]
Please post some code which creates a representative input array. Otherwise it is hard to guess, what your data are.
The below code generates the array from a .mat file attached.
clear;clc;
load('1138_bus.mat')
A=Problem.A
A1=A;
n=length(A1);
k=0;
for i=1:n
for j=1:n
if A1(i,j)~=0
k=k+1;
x(k)=i;
y(k)=j;
end
end
end
Z = compose("(%d,%d)",(x-1)',(y-1)')'
z =" [" + strjoin(Z, ',') + "]"
Jan
Jan le 22 Juil 2018
Modifié(e) : Jan le 22 Juil 2018
Why to you spend the time to create a string array, if you want to count numbers? What does "numbers" mean in this case at all? Does [(14,41)]| contain 2 ones and 2 fours or 1 time a 14 and a 41?
The conversion to a string is a confusing detail. Please explain again, which problem you want to solve actually.
If you combine the index of each element different from zero twice, the solution is trivial: all numbers appear exactly the same number of times.
By the way: Replace:
load('1138_bus.mat')
A=Problem.A
A1=A;
by
Data = load('1138_bus.mat')
A = Data.Problem.A
and use "A" instead of "A1".
Hossein
Hossein le 23 Juil 2018
The .mat file contains the adjacency matrix of a graph containing 1138 nodes. The numbers in "s" are the rows and columns of the non zero elements of that matrix. For example (23,34) means there is a nonzero element in row 23 column 34.
What I am looking for is the frequency of repetition of each node in "s". In particular, I want to have two vectors as the output: The first vector contains the nodes number, ordered from maximum repetition to the minimum repetition. The second vector corresponds to the number of repetition of each element in the first vector.
It is ok if you can find a code generating the frequency of each node directly from the .mat file.
Jan
Jan le 23 Juil 2018
I cannot open MAT files currently. It would be useful, if you simply post a small example written as Matlab code. What does s contain?
The way you create the "nodes" in z (I guess this is the "s" you are talking of) guarantees, that they are unique. So you will not find any frequency differing from 1. This means that I still do not understand, what you want to achieve.
Sorry for the confusion. Let me explain with an example. Assume that the .mat file contains the matrix A below.
A=[1 0 0 1 0 0;
1 0 0 0 0 0;
1 1 1 1 0 1;
0 0 0 0 0 0;
1 0 1 0 1 0;
0 1 0 1 1 1];
The first vector as output is the rows' number with maximum number of nonzero elements which is:
s1=[3 6 5 1 2 4]
and the second is a vector containing the number of nonzero elements in each row corresponded to row's number in s1 which is:
s2=[5 4 3 2 1 0].
The below code generates s2. I don't know how to generate s1.
A=[1 0 0 1 0 0;
1 0 0 0 0 0;
1 1 1 1 0 1;
0 0 0 0 0 0;
1 0 1 0 1 0;
0 1 0 1 1 1];
n=length(A)
for i=1:n
s1(i)=0;
for j=1:n
if A(i,j)~=0
s1(i)=s1(i)+1
end
end
end
s1=sort(s1,'descend')
Simply saying, row 3 has the most nonzero elements (5 nonzeros) so s1(1)=3 and s2(1)=5, and this continues until s1(6)=4 and s2(6)=0.
If two or more rows have the same number of nonzero elements they will be ordered from lower row to the bigger row. For example if rows 2,4,5 all have 2 nonzeros then they will be ordered in s1 as below.
s1=[ ... 2 4 5 ...]
and s2 will be like
s2=[... 2 2 2 ...].
Hope this is clarifying the issue.

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 22 Juil 2018
If you mean:
s = [1,4,2,4,4,1]
su = unique(s);
n = histc(s, su)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Tags

Question posée :

le 22 Juil 2018

Commenté :

le 24 Juil 2018

Community Treasure Hunt

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

Start Hunting!

Translated by