フィルターのクリア

Remove zeros from a vector from a specific position

2 ビュー (過去 30 日間)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 8 月 18 日
コメント済み: the cyclist 2023 年 8 月 18 日
Hello, I have the following vector as example:
X=[0, 0; 0.02, 2.3;0.04, 0.4; 0, 0; 0.06, 2.5; 0.08, 3.6; 0, 0; 0.10, 2.5]
I want to remove the zeros and I only want to keep the zeros at the beginning and obtain the following matrix:
X=[0, 0; 0.02, 2.3;0.04, 0.4; 0.06, 2.5; 0.08, 3.6; 0.10, 2.5]
I know this option
X( all(~X,2), : ) = [];
which eliminates all the zeros and then I can add the zeros again at the beggining using
X=vertcat([0 0],X);
but there must be a direct way to do it. I would appreciate the help.
  2 件のコメント
the cyclist
the cyclist 2023 年 8 月 18 日
Will there always be at least one row of zeros at the beginning?
If so, will there always be exactly one row of zeros at the beginning?
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 8 月 18 日
Yes, there will be always zeros at the beginning.

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 18 日
remove = all(~X,2);
remove(1) = false;
X(remove,:) = [];
  6 件のコメント
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 8 月 18 日
THANK YOU VERY MUCH. :).
the cyclist
the cyclist 2023 年 8 月 18 日
FYI, this solution will not work if there are multiple rows of leading zeros to keep. (That's why I asked if there was always exactly one row of leading zeros.) For example,
X = [0, 0;
0, 0;
2, 3;
5, 7;
0, 0];
X([false;all(~X(2:end,:),2)],:)=[]
X = 3×2
0 0 2 3 5 7

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by