Merging Cell Array Elements with cellstr

2 ビュー (過去 30 日間)
tinkyminky93
tinkyminky93 2022 年 6 月 9 日
回答済み: Abhijit Nayak 2022 年 6 月 9 日
Hello,
I want to merge the elements of the cell array of 100x4. After this merge, it will become 100x1. To do this process, I am using
temp = string(reshape(cell_temp, [], 4));
merged= cellstr(temp(:,1) + temp(:,2) + temp(:,3) + temp(:,4))
Are there any other methods to merge them in shorter way? Because sometimes I want to merge 15 elements but I don't want to write them explicitly. Thank you.
  2 件のコメント
KSSV
KSSV 2022 年 6 月 9 日
Show us an example with data.
tinkyminky93
tinkyminky93 2022 年 6 月 9 日
A1 A2 A3 A4 A1A2A3A4
B1 B2 B3 B4 B1B2B3B4
C1 C2 C3 C4 C1C2C3C4
...
.. -------->
.
.
F1 F2 F3 F4 F1F2F3F4

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

採用された回答

Abhijit Nayak
Abhijit Nayak 2022 年 6 月 9 日
I understand you want to automate merging of columns for a given matrix by using ‘cellstr’ function. The following code would work for any number of columns.
Create an array and assign it to ‘cell_temp’ variable in the code.
cell_temp = ones(100,5);
[row col]=size(cell_temp);
temp= string(reshape(cell_temp,[],col));
merged="";
for i=1:col
merged=merged + temp(:,i);
end
merged=cellstr(merged);
disp(merged)

その他の回答 (1 件)

Simon Chan
Simon Chan 2022 年 6 月 9 日
Try this:
merged = cellfun(@(x) cat(2,x{:}),num2cell(cell_temp,2),'UniformOutput',false)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by