フィルターのクリア

How can I convert cell array to an arrary matrix?

4 ビュー (過去 30 日間)
Abdullah Türk
Abdullah Türk 2024 年 1 月 7 日
編集済み: Stephen23 2024 年 1 月 9 日
Hi,
I have a cell array and I want to ceovert it to an array matrix.
Can I convert this cell array as follows:
array_matrix = [6 11 20 12 15 19 12 4 18 16 3 13 1 14 7 8 5 10 9 17]
Is there a way to do this?
My cell matrix is attached.

採用された回答

Dyuman Joshi
Dyuman Joshi 2024 年 1 月 7 日
移動済み: Dyuman Joshi 2024 年 1 月 7 日
in = load('cell_array.mat')
in = struct with fields:
ans: {[4×1 double] [15] [2×1 double] [4] [18] [16] [3] [2×1 double] [14] [2×1 double] [3×1 double] [17]}
x = in.ans;
out = vertcat(x{:}).'
out = 1×20
6 11 20 2 15 19 12 4 18 16 3 13 1 14 7 8 5 10 9 17
  3 件のコメント
Abdullah Türk
Abdullah Türk 2024 年 1 月 8 日
Thank you Dyuman Joshi. It works.
Dyuman Joshi
Dyuman Joshi 2024 年 1 月 8 日
You're welcome!

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

その他の回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2024 年 1 月 7 日
This is how it can be attained:
A = load('cell_array.mat').ans;
for ii = 1:numel(A)
H = A{ii};
K{ii} = cat(1, H(:)');
end
array_matrix = (horzcat(K{:}))
array_matrix = 1×20
6 11 20 2 15 19 12 4 18 16 3 13 1 14 7 8 5 10 9 17
  3 件のコメント
Walter Roberson
Walter Roberson 2024 年 1 月 8 日
temp = load('cell_array.mat');
A = temp.ans;
Abdullah Türk
Abdullah Türk 2024 年 1 月 8 日
Thank you Walter. It work now.

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


Voss
Voss 2024 年 1 月 7 日
編集済み: Voss 2024 年 1 月 7 日
@Abdullah Türk: If you know that all the cells of your cell array contain column vectors of the same class (as is the case in the variable ans in the posted mat file), then @Dyuman Joshi's approach will work:
x = load('cell_array.mat').ans;
out = vertcat(x{:}).'
out = 1×20
6 11 20 2 15 19 12 4 18 16 3 13 1 14 7 8 5 10 9 17
Alternatively, if the cells can contain arrays of any shape and dimensionality (still of the same class), then you'll need to reshape them before doing the vertical concatenation (vertcat). For example:
x = load('cell_array.mat').ans;
x_col = cellfun(@(m)reshape(m,[],1),x,'UniformOutput',false);
out = vertcat(x_col{:}).'
out = 1×20
6 11 20 2 15 19 12 4 18 16 3 13 1 14 7 8 5 10 9 17
  1 件のコメント
Abdullah Türk
Abdullah Türk 2024 年 1 月 8 日
Voss, thank you very much for your detailed information.
The following code is not work. I think this has something to do with the MATLAB version I use. I'm sure the code you wrote works, but I think it didn't work in my version.
x = load('cell_array.mat').ans;
Thanks a lot again.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by