Number of variables vary in function definition

5 次查看(过去 30 天)
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by