Removing components of a matrix without turning it into a vector

4 ビュー (過去 30 日間)
Roger L
Roger L 2017 年 10 月 3 日
編集済み: Andrei Bobrov 2017 年 10 月 3 日
Hello I'm trying to remove the Inf values from my matrix Z without turning it into a vector. I have an idea of how to do it using loops, and I know Z(1,:)=[] + Z(:,4)=[] will work as I want it to but I wanted to see if there's a quicker way because it seems like this should be a trivial thing for MATLAB. Z is actually a sample matrix, the matrix I am working with is larger and within a function, so the Inf locations will always be different.
Thanks!
Z =
Inf Inf Inf Inf
4 6 13 Inf
10 12 19 Inf
11 18 25 Inf
>> Z(Z==Inf)=[]
Z =
4 10 11 6 12 18 13 19 25

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 3 日
編集済み: Andrei Bobrov 2017 年 10 月 3 日
ii = isinf(Z);
out = Z(~all(ii,2),~all(ii));
or
ii = ~isinf(Z);
out = reshape(Z(ii),max(sum(ii)),[]);
  2 件のコメント
Roger L
Roger L 2017 年 10 月 3 日
cpaceba!
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 3 日
:) pozhaluysta!

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

その他の回答 (1 件)

KSSV
KSSV 2017 年 10 月 3 日
doc isinf . You can pick the values from Z which are not inf, instead of removing them.
iwant = Z(~isinf(Z))
  6 件のコメント
Roger L
Roger L 2017 年 10 月 3 日
Right, now I need to remove 9999 and only have the leftover matrix for further calculations. my goal is to remove the rows/cols of Inf variables and reduce the size of the matrix. meaning I want to go from a 4x4 to a 3x3.
Z =
Inf Inf Inf Inf
4 6 13 Inf
10 12 19 Inf
11 18 25 Inf
to
Z=
4 6 13
10 12 19
11 18 25
KSSV
KSSV 2017 年 10 月 3 日
Z = [ Inf Inf Inf Inf
4 6 13 Inf
10 12 19 Inf
11 18 25 Inf]
%%Remove _inf_ along rows
Z(any(isinf(Z(:,1))),:) =[] ;
%%Remove _inf_ along cols
Z = Z(:,~all(isinf(Z)));

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by