How to create a matrix with arbitrary dimensions
29 次查看(过去 30 天)
显示 更早的评论
Hi,
I like to create a matrix with arbitrary dimensions.
E.g., I need a function getMatrix(m, s), which returns a m-dimensional matrix with size s, filled with zeros or ones.
Could not working out until today, help is appreciated.
0 个评论
采纳的回答
Jos (10584)
2019-2-20
To create an zero array of an arbitray dimensions between 1 and D with arbitry sizes between 1 and S :
D = 10 ;
S = 6 ;
X = arrayfun(@(x) randi(S), 1:randi(D),'un',0)
M = zeros(X{:})
[size(M) ; [X{:}]]
2 个评论
Jos (10584)
2019-2-20
You want a Sz-by-Sz-by- ... -by-Sz array, where Dims specfies the number of Sz's
Res = zeros(repmat(Sz, 1, D))
更多回答(1 个)
Geoff Hayes
2019-2-20
Michael - this seems like a homework question so see zeros or ones for how to create a matrix of zeros or ones respectively. And see Declare function name, inputs, and outputs for how to build your function.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!