Variable number of input matrices in a function
1 次查看(过去 30 天)
显示 更早的评论
I want to change the code below so the function blkdiag can take on any number of matrices A, based on the value of N.
N = 3;
A_1 = sparse(1:N,1:N,-1*ones(N,1),N,N+1);
A_2 = sparse(1:N,2:N+1,1*ones(N,1),N,N+1);
A = A_1+A_2;
out = full(blkdiag(A,A,A))
So suppose N=4, then
out = full(blkdiag(A,A,A,A))
And so on.
How can I create a variable input for this function, based on a value?
Thanks,
Tim
3 个评论
采纳的回答
Guillaume
2019-2-4
N = 4;
A_1 = sparse(1:N,1:N,-1*ones(N,1),N,N+1);
A_2 = sparse(1:N,2:N+1,1*ones(N,1),N,N+1);
A = A_1+A_2;
blkdiaginputs = repelem({A}, N); %put your inputs in a cell array
out = full(blkdiag(blkdiaginputs{:})) %and convert the cell array into a comma-separated list
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!