remove all strings from nested cell array

2 ビュー (過去 30 日間)
Jo ali
Jo ali 2022 年 6 月 22 日
コメント済み: Jo ali 2022 年 7 月 7 日
Hi,
I'm trying to remove all string/chars from a 4x10 cell array where each cell in the array contains a 40x1 column vector (so as you can see, it's nested). Most commands I usually use such as cell2mat don't work on this because the cell array is nested.
Or, if preferred, is there a way to read in all the elements of a certain column in one of the cells in a cell array apart from the elements containing strings? e.g:
col=rand(4,10) %matrix containing column indices
for i=1:4
for j=1:10
new_cell_arrray{i,j}=g{i,1}(:,col(i,j)); %g is another (existing) 4x1 cell array and new_cell_array is 4x10.
end
end
Thank you
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 6 月 22 日
If you "remove" an entry from a 2D array, the result cannot be a 2D array any more.

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

採用された回答

Walter Roberson
Walter Roberson 2022 年 6 月 22 日
C = num2cell(randi(9,4,10));
C(randperm(numel(C), 4)) = {"string"};
C(randperm(numel(C), 4)) = {'chars'};
C
C = 4×10 cell array
{[5]} {[ 8]} {[5]} {[6]} {[1]} {[ 8]} {'chars'} {'chars' } {["string"]} {'chars'} {[3]} {["string"]} {[4]} {[1]} {[2]} {["string"]} {[ 1]} {[ 9]} {[ 5]} {[ 8]} {[8]} {[ 5]} {[4]} {[8]} {[9]} {[ 2]} {[ 1]} {[ 5]} {[ 6]} {[ 7]} {[3]} {[ 3]} {[5]} {[1]} {[5]} {[ 2]} {'chars'} {["string"]} {[ 9]} {[ 8]}
for K = 1 : size(C,2)
mask = ~cellfun(@(c)ischar(c)||isstring(c), C(:,K));
subset{K} = C(mask, K);
end
subset
subset = 1×10 cell array
{4×1 cell} {3×1 cell} {4×1 cell} {4×1 cell} {4×1 cell} {3×1 cell} {2×1 cell} {2×1 cell} {3×1 cell} {3×1 cell}
subset{1}, subset{2}
ans = 4×1 cell array
{[5]} {[3]} {[8]} {[3]}
ans = 3×1 cell array
{[8]} {[5]} {[3]}
  5 件のコメント
Jo ali
Jo ali 2022 年 7 月 7 日
Thank you so much! it worked :D

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

その他の回答 (0 件)

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by