remove the zeros in the Matrix
2 ビュー (過去 30 日間)
古いコメントを表示
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
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
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)
x = z(2:2:end,2:2:end)
FWIW, the reason your code doesn't work is because you never reset n
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!