could anyone help me to solve the issue in the following command

2 次查看(过去 30 天)
when i run the command
B = arrayfun(@(N) randi(2,N), repelem((1:3).',5), 'uniform', 0)
i am getting B = 15×1 cell array
{[ 1]}
{[ 1]}
{[ 1]}
{[ 2]}
{[ 2]}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{2×2 double}
{3×3 double}
{3×3 double}
{3×3 double}
{3×3 double}
{3×3 double} and the data inside are
3
2
2
2
2
[1,1;1,1]
[1,1;2,1]
[1,3;3,1]
[3,1;1,3]
[1,1;1,1]
[1,1,3;1,2,2;1,2,1]
[3,2,2;3,1,3;1,2,3]
[1,2,2;1,3,1;1,2,3]
[3,1,1;3,3,1;3,2,3]
[3,2,1;3,1,1;3,2,1]
but I actually need B =
15×1 cell array
{1×1double}
{1×1 double}
{1×1 double}
{1×1 double}
{1×1 double}
{1×2 double}
{1×2 double}
{1×2 double}
{1×2 double}
{1×2 double}
{1×3 double}
{1×3 double}
{1×3 double}
{1×3 double}
{1×3 double} containing the following values
3
2
2
2
2
[1,1]
[1,1]
[1,3]
[3,1]
[1,1]
[1,1,3]
[3,2,2]
[1,2,2]
[3,1,1]
[3,2,1]
could anyone please help me to get it.

采纳的回答

Walter Roberson
Walter Roberson 2021-6-23
B = arrayfun(@(N) randi(2,1,N), repelem((1:3).',5), 'uniform', 0)
  2 个评论
jaah navi
jaah navi 2021-6-24
If i run the above the command it generates 15x1 cell array. thanks for it.
But i need to have for row having 1x1 double the value should be of 1 and not more than 1 and for 1x2 the value should be of [1,2 ], [1,1], [2,1] and not more than 2, [2,2]...
for 1x3 double the value should be [1,2,3], [3,2,1], [1, 2,2] and not more than 3 and should ignore [1,3,3] as it leaves 2.
Could you please help me to write how should I need to cell array in the above mentioned manner.
Walter Roberson
Walter Roberson 2021-6-24
You have a fixed outcome, and you have no explainable reason for that particular outcome. You should just write static assignment statements.
Remember the fundamental rule of practical programming: get it working first, and worry about making it shorter or faster later. You have been held up for days trying to write these things in a compact way, when you could have just written something that got the required outcome and moved on to the next step.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by