Repeated values in matrix

6 visualizzazioni (ultimi 30 giorni)
Rachel Ramirez
Rachel Ramirez il 13 Gen 2021
Commentato: Rachel Ramirez il 20 Gen 2021
Hi,
I have no idea how to approach this problem and needed some guidance. Any help would be appreciated.
Say I have a matrix/array similar to Table_1. How can I go about finding the first 5 numbers that repeat at least twice only on the FIRST column? Once found a new matrix would be generated where all values from the original matrix would be included up until the 5th value from the FIRST column that repeats at least twice is found similar to Table_2. I have no idea if this is even possible to do. Keep in mind this is just an example, matrix could be larger or smaller so I would prefer not to set a value.
Please refer to images. Thank you.
I would like to keep them as array/matrix and not use cells.
  2 Commenti
David Hill
David Hill il 13 Gen 2021
Why not stop at [7 6] since it is the fifth value that repeats twice?
Rachel Ramirez
Rachel Ramirez il 13 Gen 2021
Modificato: Rachel Ramirez il 13 Gen 2021
oh yes! that was an error from my part. it would stop at [7 6]. Here is an updated version. I was actually thinking, I could either include all values that are in b/w even though they don't meet the condition of repeating or just generate matrix that meets the condition. Does it make sense?

Accedi per commentare.

Risposta accettata

Prudhvi Peddagoni
Prudhvi Peddagoni il 19 Gen 2021
Modificato: Prudhvi Peddagoni il 19 Gen 2021
Hi,
You can define a variable called freq, to store the frequency of the a number. Frequency of number is stored in index. You will go through the matrix until atleast 5 of these indices has a value greater than 2.
freq = zeros(100,1); %assuming the numbers in the matrix won't be greater than 100
for i = 1:length(M)
freq(M(i,1)) = freq(M(i,1))+1;
if nnz(freq>=2) >= 5
break
end
end
%now we need to remove numbers which have frequency lessthan 2
freq(freq<2) = 0;
Now you need to iterate through the original matrix . Let x be the number present in the first column. You will include this row in the final matrix if freq(x) is not equal to zero. You iterate through the original matrix until you reach the row (variable i).
Hope this helps.
  3 Commenti
Prudhvi Peddagoni
Prudhvi Peddagoni il 19 Gen 2021
Thanks for the help madhan. I have edited the typo.
Rachel Ramirez
Rachel Ramirez il 20 Gen 2021
Thank you for the explanation. I tested out and seems to be working well. I'm going to see how to get the x values from the first column.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by