フィルターのクリア

Recompose stacked column cell vectors into adjacent row cell vectors

1 回表示 (過去 30 日間)
FM
FM 2022 年 4 月 6 日
コメント済み: Voss 2022 年 4 月 6 日
I have a row array of cells, varargin, within a function. The contents of each cell corresponds to an argument supplied by the invoker. I know that each argument is a column vector, and the vectors are of the same height. In my particular scenario, they are the input variables of a table when "rowfun" is applied (with "Grouping", though I'm not sure that matters). The type/class of each argument can be anything, including cells, strings, cells containing char arrays, objects of type OptimizationVariable or OptimizationExpression, etc.
I would like to decompose the vertical arrays and recompose them into row cell arrays. For example, if varargin is:
{ [1;2] ["cat";"dog"]}
then I want
{ 1 "cat" ; 2 "dog" }
I know that I can loop through the height of the arguments, then loop through the arguments to buildup each row. I'm looking for vectorized way to do this. If I try [varargin{:}], I get unwanted type conversion, leading to a "3x2 string array":
"1" "cat"
"2" "dog"
Thanks for any suggestion on how to do this while preserving the data type/class. If this is not possible, thanks for letting me know.

採用された回答

Voss
Voss 2022 年 4 月 6 日
Here's one way:
varargin = { [1;2] ["cat";"dog"] };
% convert each element of varargin to a (2x1) cell array:
temp = cellfun(@num2cell,varargin,'UniformOutput',false);
% concatenate the resulting (2x1) cell arrays into a (2x2) cell array:
temp = [temp{:}]
temp = 2×2 cell array
{[1]} {["cat"]} {[2]} {["dog"]}
temp{1,1}
ans = 1
temp{1,2}
ans = "cat"
temp{2,1}
ans = 2
temp{2,2}
ans = "dog"
  2 件のコメント
FM
FM 2022 年 4 月 6 日
Thanks! I should have known that there was a way!
Voss
Voss 2022 年 4 月 6 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by