if statement does not execute the statements

조회 수: 1 (최근 30일)
Maria445
Maria445 2017년 10월 14일
댓글: Star Strider 2017년 10월 14일
I can't understand what I'm doing wrong here; basically I have two matrices, A and B, and if one of the two matrices has a row in which every entry is equal to NaN, I want to delete the corresponding row in the other matrix as well. This is the code I used:
A1 = A;
B1 = B;
for i = 1 : size(A,1)
if sum(isnan(A(i,2:end)))==size(A(:,2:end),2) || ...
sum(isnan(B(i,2:end)))==size(B(:,2:end),2);
A1(i,:) = []
B1(i,:) = [];
end
end
However, in matrix B1 there are still many rows entirely made of NaN values. Why is this not working?
  댓글 수: 1
Maria445
Maria445 2017년 10월 14일
Also, every time I run the for loop again, matrices A1 and B1 reduce their dimension...this shouldn't happen

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

채택된 답변

Star Strider
Star Strider 2017년 10월 14일
If I understand correctly what you want to do, this should work:
A = randi(9, 7, 4); % Create Matrices
B = randi(9, 7, 4); % Create Matrices
A([2 6],:) = NaN; % Create ‘NaN’ Rows In ‘A’
B(4,:) = NaN; % Create ‘NaN’ Rows In ‘B’
row_idx = all(isnan(A),2) | all(isnan(B),2); % Logical Index For ‘NaN’ Rows In Both
A(row_idx,:) = []; % Delete Rows In ‘A’
B(row_idx,:) = []; % Delete Rows In ‘B’
  댓글 수: 2
Maria445
Maria445 2017년 10월 14일
Yes! This is exactly what I wanted to do, thank you so much!! :)
Star Strider
Star Strider 2017년 10월 14일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by