How to Erase a Row in a Matrix, if It has Complex Numbers?
조회 수: 11 (최근 30일)
이전 댓글 표시
I have a table in matrix form (n row,10 column). The n row might changed according to previous codes, it is not constant.
I would like to check whether there is any complex number in the table, then if there is a complex number in any row, I would like to erase all that row. (There would be less rows than n rows).
How can I do this? My code is below:
Table= [1 2 3 4 5 6 7 8 9 10; 1+i 2 3 4 5 6 7 8 9 10; 10 9 8 7 6 5 4 3 2 1]; % Table row size is not fixed, not always 3.
% Therefore I find its size here below
[size1,size2]=Table
k=0;
for i=1:1:size1;
if imag(Table(i,1))~=0 | imag(Table(i,2))~=0 | imag(Table(i,3))~=0 | ...
imag(Table(i,4))~=0 | imag(Table(i,5))~=0 | imag(Table(i,6))~=0 | ...
imag(Table(i,7))~=0 | imag(Table(i,8))~=0 | imag(Table(i,9))~=0 | ...
imag(Table(i,10))~=0;
k=k+1;
rows_would_be_deleted(k,1)=i;
end
end
Table_old=Table;
Table(rows_would_be_deleted(:,1),:)=[];
Table_new=Table;
This code works, if there is any complex number in the table. However, it should keep the table, if there was no complex number. But, if there is no complex number, the if block in my code isn't executed. And it gives error with unknown variable/function "rows_would_be_deleted".
How can I fix this? Any simpler code is also welcomed. Thanks in advance.
댓글 수: 0
채택된 답변
Walter Roberson
2021년 2월 7일
Table= [1 2 3 4 5 6 7 8 9 10; 1+i 2 3 4 5 6 7 8 9 10; 10 9 8 7 6 5 4 3 2 1];
mask = any(imag(Table),2);
Table(mask,:) = [];
Table
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!