How to filter data in a variable based on its value

61 ビュー (過去 30 日間)
Nom
Nom 2020 年 5 月 7 日
コメント済み: Star Strider 2020 年 5 月 7 日
Hello,
I have one 100 by 3 array which contains random values from 0 to 1000
I wanted to filter the data in a new array so that only values above 500 are shown.
To do this I did
A = randi([0 1000],100,3) %Generates random numbers from 0 to 1000 in a 100by3 array
A(A>500) %This is the area I need help
In the code above, A(A>500) only shows me values from the first column of A which are greater than 500.
How can I extend this so that A(A>500) will generate a x by 3 array?

採用された回答

Star Strider
Star Strider 2020 年 5 月 7 日
This will set the elements that are less than or equal to 500 to 0:
A = A.*(A>500);
It preserves the structure of ‘A’.
  2 件のコメント
Nom
Nom 2020 年 5 月 7 日
Thank you so much!
I spent way too long on this, I was attempting to do it on cellfun as well but wasn't able to get that working.
A = cellfun(@(x) A>500,A,'UniformOutput',0);
Just out of cursiosity would you happen to know the cellfun equivalent? (Assuming A is a cell of course)
Star Strider
Star Strider 2020 年 5 月 7 日
Try this:
A = randi([0 1000],100,3); %Generates random numbers from 0 to 1000 in a 100by3 array
% A(A>500) %This is the area I need help
Ac = mat2cell(A, ones(1,size(A,1)), ones(size(A,2),1)); % Create Cell Array
ix = cell2mat(cellfun(@(x)x>500,Ac, 'Uni',0)); % Logical Index Matrix
Ac(~ix) = {[]}; % Output
My intent was to have the elements that did not meet the criterion be empty [] elements. They are 0 instead. That may reflect a difference in how cell arrays are handled in R2020a.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by