Remove some elements from a matrix but conserve the same struct

i have the matrix
x=[0 -1 0 5 10 5 0 0;
0 0 5 10 20 10 5 0;
1 -1 0 5 10 5 2 0;
0 2 1 0 5 2 1 0;
0 0 0 -10 0 0 0 0;]
i would like to "disappear" the values between -1 and 5 and keep the matrix with same struct but only the wanted values. the new matrix going to be something like this:
x=
[ 10 ;
10 20 10 ;
10 ;
;
-10 ;]

 採用された回答

Matt Fig
Matt Fig 2011 年 3 月 29 日

0 投票

You will have to use cell arrays if you want to preserve the shape:
X = num2cell(x); % A cell array with the same shape as x.
X(x<=5 & x>=-1) = {[]}
Is there a practical reason for doing this? It seems like a large waste of memory to me. If you need to keep the indices viewable, you also could do this:
X = x;
X(x<=5 & x>=-1) = 0;
X = sparse(X)

1 件のコメント

Rafael Freire
Rafael Freire 2011 年 3 月 29 日
i have a surface [1023x1023 double] and i want to keep only the peaks that is above and under the values to reduce the number of points, after do all of thar i want to mesh the answer and thaks for you answer

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

その他の回答 (2 件)

Sean de Wolski
Sean de Wolski 2011 年 3 月 29 日

2 投票

Rafael, then set everything in the range you described to NaN, and mesh will pretend they're not there.
X(X>=-1&X<=5) = nan;
mesh(X);
You can reduce the size of the matrix first by zeroing out any rows/columns that contain no "peaks". Use the ANY function and the dimensional option to do this.

3 件のコメント

Rafael Freire
Rafael Freire 2011 年 3 月 29 日
Thanks it was exactly what i need
Matt Fig
Matt Fig 2011 年 3 月 29 日
+1 Sean!
@ Rafael I hope next time you will include the "why" when asking about the "how!" ;-)
Rafael Freire
Rafael Freire 2011 年 3 月 29 日
Ok, sorry about that i should be more specific. Next time i will inlclude the why. Thanks for the advice and attention.

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

Sean de Wolski
Sean de Wolski 2011 年 3 月 29 日

0 投票

x2 = cellfun(@(x)x(x>5|x<-1),num2cell(x,2),'uni',false)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by