Finding the frequency of each number in a string
Afficher commentaires plus anciens
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
Jan
le 22 Juil 2018
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.
Hossein
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
le 23 Juil 2018
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.
Hossein
le 24 Juil 2018
Réponses (1)
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!