rows and columns in array

1 vue (au cours des 30 derniers jours)
Rachel Ramirez
Rachel Ramirez le 3 Déc 2020
Say I have an array with x amount of rows and 3 columns.
I want to count how many times does a specifc value repeat on the first column. So for example I want to know how many times do numbers 5,6,7,8,9 repeat in the first column. If they repeat less than 4 times then I want to delete the entire row where that value is in the first and/or second column to be deleted.
example: 5 has less than 4 combinations then the following rows would be deleted from the array.
5 1 100
2 5 800
5 5 500
  1 commentaire
Image Analyst
Image Analyst le 3 Déc 2020
Well fine, but you didn't show us the original matrix with the repeats and the "rows to be removed" in that matrix. What does your original matrix look like? and do you want 5,6,7,8,9 in order and that whole stretch of 5 adjacent numbers is repeated more or less than 4 times? Or do you want to delete rows where 5 is less than 4 times, and 6 is less than 4 times, etc.? The meantime, see ismember(), sum(), etc. And does repeated mean adjacent repeats, like [1,4,4,4,4,5,6] or can the repeat be separated, like [1,4,3,4,6,4,7,4]?

Connectez-vous pour commenter.

Réponse acceptée

Swetha Polemoni
Swetha Polemoni le 7 Déc 2020
Hi Rachel Ramirez,
It is my understanding that you want
  • To check how many times a number repeats in a particular row or column.
  • Delete that row or coumn if the count is less than 4.
A=randi(5, 10 ,10); % generating a random matrix with dimension 10X10
j=1;
for i= 1:10
count=sum(A(j,:)==5); % counting number of times 5 is repeting in jth row
if(count<4)
A(j,:)=[]; % deleting jth row if count is less than 4
j=j-1;
end
j=j+1;
end
Hope this helps.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by