Question with vectorisation?

2 ビュー (過去 30 日間)
Manne Plok
Manne Plok 2018 年 9 月 6 日
編集済み: Stephen23 2018 年 9 月 6 日
Hello This is the code I have.
n = 1:1000
A(n,1) = ceil(rand*5);
What this does is it finds a random value between 1 and 5, and places that same value in all rows 1 to 1000, col 1 of A. This is not what I wanted it to do. I wanted it to put a different random number each time for each row of A from 1 to 1000. I want to use vectorisation (and not for loops) because otherwise it is too slow. What step am I missing?

回答 (1 件)

Stephen23
Stephen23 2018 年 9 月 6 日
編集済み: Stephen23 2018 年 9 月 6 日
Just use randi and define the output size:
A(1:1000,1) = randi(5,1000,1)
You could do the same with rand, but this is just more complex with no benefit:
A(1:1000,1) = ceil(5*rand(1000,1))

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by