Creating an interesting matrix
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
Hello,
I want to ask you for some help with writing a function to get the following matrix:
- Dimensions (a^b) x b (defined as an input to a function)
- The matrix has a following structure (let's say a=4, b=3):
(1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
...
3 3 3)
Writing this matrix for known dimensions is easy (just use for loops) but my task is to have arbitrary a and b values.
Many thanks in advance,
Miko
0 个评论
回答(1 个)
It's not clear in your example where a = 4 has any influence on the output. Perhaps your matrix is supposed to go all the way up to 4 4 4.
In that case, for a up to 10, it's easy to accomplish with dec2base.
a = 4;
b = 3;
out = dec2base(0:a^b-1, a) - '0' + 1
Another way to accomplish the same with no limitation to the range of a (bar memory available) is with ndgrid:
a = 4;
b = 3;
out = cell(1, b);
[out{:}] = ndgrid(1:a);
out = reshape(cat(b+1, out{:}), [], b) %fliplr if you want same ordering as your example
0 个评论
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!