Azzera filtri
Azzera filtri

frequency of 1, 2, 3rd most occurring number

1 visualizzazione (ultimi 30 giorni)
I have a 27*792 matrix which contains 1:27 numbers in random order. I am finding column wise mode and saving them in vector a so that i can find which number has maximum occurence in 1st column, 2nd column and so on. If the mode of the first and second column is same, i want to replace that number by the second most frequent number in from the column array. In case, the second number also exist, i want to find the next frequent number. In short, the final array which i get should have unoque pattern of numbers 1:27 in specific order. I have tried to code it:
ranks = randi([1 27],27,792,'double'); % create array for
a = zeros(1,27); % initialize vector
new_a = zeros(1,27); % initialize vector
for i = 1:27 % for n columns
X = ranks(i,:); % get ith column
m = mode(X) % find mode
a(i) = m; % save mide of ith column
new_a = a;
if any(new_a(1:i-1) == a(i)) % check if the mode m exists in ith-1 iterations
X(X==m) = NaN ; set the mode to nan
m1 = mode(X) % find new mode1
a(i) = m1;
new_a = a;
if any(new_a(1:i-1) == m1) % if mode m1 exists in ith-1 iteration
X(X==m1,m2) = NaN ;
m1 = mode(X) save matrix
end
end
end

Risposta accettata

Voss
Voss il 5 Feb 2022
Maybe this will work
ranks = randi([1 27],27,792,'double'); % create array for
a = zeros(1,27); % initialize vector
for i = 1:27 % for n rows
X = ranks(i,:); % get ith row
m = mode(X); % find mode
while any(a(1:i-1) == m) % check if the mode m exists in iterations 1 through i-1
X(X==m) = NaN; % set the mode to nan
m = mode(X); % find new mode
if isnan(m)
% if all elements of X have been replaced with NaNs there is
% nothing else to do but let the mode for this row be NaN
break
end
end
a(i) = m; % save mode of ith row
end
disp(a);
14 17 12 20 16 5 11 21 1 3 9 22 8 6 24 27 23 26 19 2 13 7 10 25 15 18 4
numel(a) == numel(unique(a))
ans = logical
1

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by