rows and columns in array

조회 수: 2 (최근 30일)
Rachel Ramirez
Rachel Ramirez 2020년 12월 3일
답변: Swetha Polemoni 2020년 12월 7일
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
Image Analyst
Image Analyst 2020년 12월 3일
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]?

댓글을 달려면 로그인하십시오.

채택된 답변

Swetha Polemoni
Swetha Polemoni 2020년 12월 7일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by