フィルターのクリア

Concatenating vectors based on list of variable names

3 ビュー (過去 30 日間)
Jerki Jokne
Jerki Jokne 2020 年 3 月 17 日
コメント済み: Jerki Jokne 2020 年 3 月 17 日
I have 5 column vectors in my workspace with the same length, and a cell array with their names in the order that I would like to horizontally concatenate them in. In this example I use 5 vectors, but there can be more.
a1 = rand(5,1);
a10 = rand(5,1);
a11 = rand(5,1);
a2 = rand(5,1);
a3 = rand(5,1);
order = {'a1', 'a2', 'a3', 'a10', 'a11'};
How can I horizontally concatenate the variables in this order, such that I have the following result?
[a1, a2, a3, a10, a11]

採用された回答

BobH
BobH 2020 年 3 月 17 日
編集済み: BobH 2020 年 3 月 17 日
c = cellfun(@eval, order, 'un', 0);
r = vertcat( c{:} );
% horzcat if that's what you wanted, and all variables in order are same size
vertical concatenation looks like this (semicolons not commas)
[a1; a2; a3; a10; a11]
  1 件のコメント
Jerki Jokne
Jerki Jokne 2020 年 3 月 17 日
Sorry, I meant horizontally. I edited the question to be consistent.

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

その他の回答 (1 件)

Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 17 日
Hi Jerki,
This can be done with a for loop as shown below:
out =[]
for i = 1:numel(order)
out = [out eval(order{i})];
end
Hope this helps.
Regards,
Sriram

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by