How can I create n arrays of size 100 each with random integer values?

20 次查看(过去 30 天)
c = cell(1,100)
for i = 1:100
c{i} = randi(100)
end
I tried doing this but it only gives 100 arrays of size 1.
I need 1-D arrays
Any other solution is gladly welcomed.
Thank you!

采纳的回答

Alex Mcaulley
Alex Mcaulley 2019-12-13
c = cell(1,100)
for i = 1:100
c{i} = randi(100,1,100)
end
  7 个评论
Alex Mcaulley
Alex Mcaulley 2019-12-13
It is simpler with @Guillaume suggestion, creating 100x100 matrix and then, each row (or each column) is an array of 100 elements:
result = randi(50, 100)
result(1,:) %1x100 array

请先登录,再进行评论。

更多回答(2 个)

Bhaskar R
Bhaskar R 2019-12-13
c = cell(1,100);
for i = 1:100
c{i} = randperm(100);
end

Steven Lord
Steven Lord 2019-12-13
Since all of your arrays are the same size, I would consider stacking them in one of the dimensions in which they have size 1.
A = randi([-10 10], 5, 8);
In this example A contains 5 "arrays" (the rows) of random integer values between -10 and 10, each "array" having size [1 8]. To use the third "array":
third = A(3, :)
For 2-dimensional arrays, you could stack them in the third dimension like pages in a book.
B = randi([-10 10], 3, 3, 7);
fifth = B(:, :, 5)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by