removing specific values in a cell array

1 回表示 (過去 30 日間)
HYZ
HYZ 2020 年 5 月 19 日
コメント済み: HYZ 2020 年 5 月 19 日
Hi,
I have a cell array A = {[1:10] [1:10]};
I would like to remove any elements which are less than 3 and greater than 8 in all vectors in the cell array.
The result is A = {[3:8] [3:8]}; Could you please help? Thanks.

採用された回答

Stephen23
Stephen23 2020 年 5 月 19 日
>> A = {1:10,1:10}; % square brackets are not required.
>> F = @(a)a(a>=3&a<=8);
>> B = cellfun(F,A,'uni',0);
>> B{:}
ans =
3 4 5 6 7 8
ans =
3 4 5 6 7 8
  1 件のコメント
HYZ
HYZ 2020 年 5 月 19 日
Thanks a lot!

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

その他の回答 (1 件)

Stanislao Pinzón
Stanislao Pinzón 2020 年 5 月 19 日
Maybe something like this
A = {1:10,1:10};
A{1}(A{1}<3|A{1}>8) = [];
A{2}(A{2}<3|A{2}>8) = [];
Naturally, it would be tedious in wide cell arrays. So you can use instead:
A = {1:10,1:10};
for i=1:length(A)
A{i}(A{i}<3|A{i}>8) = [];
end
  1 件のコメント
HYZ
HYZ 2020 年 5 月 19 日
Thanks a lot!

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

カテゴリ

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