How to assign values to an array with broadcasting

Is there any way to do broadcasting while assigning values to an array?
I know how to broadcast a computation:
G = zeros(3)
G + [1 2 3]
ans =
1 2 3
1 2 3
1 2 3
But I want to assign the above values to G directly:
G(:,:) = [1 2 3]
Unable to perform assignment because the size of the left side is 3-by-3 and the size of the right side is 1-by-3.
I know how to broadcast a simple scalar value:
G(:) = 1
G =
1 1 1
1 1 1
1 1 1
And I realise I could do this instead:
G(:,:) = repmat([1 2 3], 3, 1)
G =
1 2 3
1 2 3
1 2 3
But I wondered if there is a simpler way to assign a vector of values to all the rows using automatic broadcasting.
Judging by the answers to this question, I am guess not, but I wanted to check.

 採用された回答

madhan ravi
madhan ravi 2020 年 7 月 28 日
編集済み: madhan ravi 2020 年 7 月 28 日

0 投票

Your way of using repmat() is the easiest way.

9 件のコメント

madhan ravi
madhan ravi 2020 年 7 月 28 日
編集済み: madhan ravi 2020 年 7 月 28 日
Why not simply
G = zeros(3) + (1:3) % ?
Or
kron(repelem(1,3,1),1:3)
Bill Tubbs
Bill Tubbs 2020 年 7 月 28 日
編集済み: Bill Tubbs 2020 年 7 月 28 日
The reason I want to avoid G = zeros(3) + (1:3) is for performance reasons—to avoid the possibility that it destroys the existing memory for G and reallocates it. I'm used to Python where you always do: G[:] = [1,2,3] (assign new values) if G is very big rather than: G = np.repeat([[1, 2, 3]],3,axis=0) (create a new array in memory).
Bill Tubbs
Bill Tubbs 2020 年 7 月 28 日
編集済み: Bill Tubbs 2020 年 7 月 28 日
Seems like G = repmat([1 2 3], 3, 1) is the best solution. As far as I can see, none of these methods assign new values directly to the original array without first creating a whole new array of the same size for the right-hand-side (so there is no point in doing G[:,:] = ...). I ran some speed tests with a large G array and the repmat method is the fastest, followed by kron(repelem(n,10,1),1:10). Thanks!
madhan ravi
madhan ravi 2020 年 7 月 28 日
Ah perhaps the implicit expansion:
zeros(3, 1) + (1:3)
Bill Tubbs
Bill Tubbs 2020 年 7 月 28 日
Interesting result I just discovered:
G = zeros(500000,10)
is about twice as fast as
G(:) = 0
(for the same size G). So I guess, assignment of new values is slower in MATLAB than building a new array anyway!
madhan ravi
madhan ravi 2020 年 7 月 28 日
And?
Bill Tubbs
Bill Tubbs 2020 年 7 月 28 日
It's surprising* and it means that my idea of avoiding allocating new memory by somehow assigning to the original array probably wouldn't have speeded up the code even if it were possible (* Example: allows a 1.6x speedup in Python).
AB WAHEED LONE
AB WAHEED LONE 2021 年 3 月 8 日
For any dimension matrix,you can do
X=repmat(fading,20,1)
where fading is a matrix of any size. For me it was fading coefficient matrix
Walter Roberson
Walter Roberson 2021 年 3 月 8 日
repmat was discussed above. Bill was hoping for something more efficient.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by