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!
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!