Borrar filtros
Borrar filtros

How to Erase a Row in a Matrix, if It has Complex Numbers?

8 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Feb. de 2021
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
Table = 2×10
1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by