Number of variables vary in function definition

6 views (last 30 days)
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!

Accepted Answer

the cyclist
the cyclist on 5 Feb 2020
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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!