How can i remove an entire row or column of a matrix if the elements of the matrix in the row or column of the matrix has been flagged with a certain number?

조회 수: 13 (최근 30일)
Hi everyone, I have a matrix T(150,63051) and another matrix Flag(150,63051) which is the flag matrix for T. now I want to remove the entire row from matrix T if the corresponding elements (one or more than one element in the row) has flagged greater than 2 in the Flag matrix. here is my script:
for i=1:150
for j=1:length(T)
if (flag(i,j)>2)
T(i,:)=[];
end
end
end
but it gives me below error: Matrix index is out of range for deletion.
can anyone please help me to resolve this problem best regards,

채택된 답변

Guillaume
Guillaume 2018년 5월 14일
First, a big warning, don't use length (for vectors numel is better) and certainly don't use length on a matrix. On a matrix use size with an explicit dimension. I also don't understand why you hardcode one dimension and retrieve the other, why not retrieve both:
for row = 1:size(T, 1)
for col = 1:size(T, 2)
is a lot safer than what you wrote. In any case, your loop code is very flawed. Never delete elements of an array when you iterate over it since your index gets out of sync with the actual rows. E.g, at i = 8 you delete row 8. row 9 is now row 8, row 10 is now row 9, etc. You then increase i, which becomes 9, and test flag(9, j) but row 9 of T is what used to be row 10. Out of sync.
Anyway, to answer your question, it's simply:
T(any(flag > 2, 2), :) = [];

추가 답변 (1개)

Bob Thompson
Bob Thompson 2018년 5월 14일
You can use smart indexing to complete this, but you're going to have a challenge with different size matrices if you aren't careful.
results = T(Flag>2);
This will produce a single column matrix with all elements of T where corresponding Flag elements are greater than 2. If you want to organize this into different columns then I would suggest keeping the first for loop you have set up.
for k = 1:150;
results(k,:) = T(k,B(k,:)>2); % Technically this looks row by row, rather than column by column.
end
The challenge with this setup is that any time the matrix needs to grow longer, because a previous row has less retained digits than the current, then an error will be thrown. Similarly, if a previous row was longer than the current then a dimension mismatch error will occur.
If you want to simply replace all values that don't meet the flag with blanks then I would suggest:
T(Flag<=2) = NaN;
This will prevent matrix sizing issues, and should solve the current issue you're having.
  댓글 수: 1
Joseph
Joseph 2018년 5월 14일
Thanks for your answer. I already used T(Flag<=2) = NaN; in my script. the problem is that it only makes the element with Flag>2=nan; however, I want to make the entire row nan or [] if even one element is (flag>2).

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by