remove the zeros in the Matrix

조회 수: 2 (최근 30일)
Ali ALATAWI
Ali ALATAWI 2021년 11월 19일
답변: DGM 2021년 11월 20일
how to remove the zeros from matrix x??
clear all
clc
n=1;
m=1;
z=randi([1 10],5,10)
for i=1:5
if(mod(i,2)==0)
for j=1:length(z)
if(mod(j,2)==0)
x(m,n)=z(i,j)
n=n+1;
end
end
m=m+1;
end
end
  댓글 수: 1
dpb
dpb 2021년 11월 19일
Depends on what you mean by "remove". A matrix cannot be anything but a rectangular array, so you simply cannot "remove" elements leaving "holes" in a regular array. A cell array also has to be regular, but it does have the feature that a cell may contain the empty indicator. For a regular numeric array, you can use NaN or Inf as missing value indicators, but you can't not have something in every location.

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

답변 (1개)

DGM
DGM 2021년 11월 20일
If all you want is the intersection of all even rows and columns, just use indexing.
z = randi([1 10],5,10)
z = 5×10
2 7 1 1 2 5 2 6 8 4 1 5 1 4 8 10 1 9 2 9 9 3 5 8 6 6 6 6 5 8 2 2 3 1 8 3 1 9 3 5 4 1 1 8 8 5 6 6 5 7
x = z(2:2:end,2:2:end)
x = 2×5
5 4 10 9 9 2 1 3 9 5
FWIW, the reason your code doesn't work is because you never reset n

카테고리

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

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by