Number of variables vary in function definition

5 ビュー (過去 30 日間)
Lenilein
Lenilein 2020 年 2 月 5 日
コメント済み: Lenilein 2020 年 2 月 5 日
Hello,
I tried looking at following problem under all angles but couldn't find a solution: I have a matrix A with n number of rows and p columns. Whereas the number of columns is fixed, the number of rows is dependant on a parameter. I want to concatenate the values of each column to one element and this formula would give me the output that I wish if the number of rows is 5:
X = arrayfun(@(x,y,z,w,v) (strcat(num2str(x),num2str(y),num2str(z),num2str(w),num2str(v))),A(1,:),A(2,:),A(3,:),A(4,:),A(5,:),'un',0);
However, in my case the number of rows varies and therefore the number of variables x,y etc. vary as well.
Do you know a way to automatically adjust the number of variables and the rest of parameters (num2str(x), A(1,:), etc.) accordingly? I suspect this could be possible with a loop but couldn't find the way to do it.
Thanks!

採用された回答

the cyclist
the cyclist 2020 年 2 月 5 日
Here is a different approach, using a loop and sprintf:
% Count columns and preallocate X
numberCols = size(A,2);
X1 = cell(1,numberCols);
% Fill cell with desired strings
for nc = 1:numberCols
X1{nc} = sprintf('%d',A(:,nc));
end
  1 件のコメント
Lenilein
Lenilein 2020 年 2 月 5 日
Thank you, it works great + I got to know a new function! :)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by