How to assign values to an array with broadcasting

83 visualizaciones (últimos 30 días)
Bill Tubbs
Bill Tubbs el 28 de Jul. de 2020
Comentada: Walter Roberson el 8 de Mzo. de 2021
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.

Respuesta aceptada

madhan ravi
madhan ravi el 28 de Jul. de 2020
Editada: madhan ravi el 28 de Jul. de 2020
Your way of using repmat() is the easiest way.
  9 comentarios
AB WAHEED LONE
AB WAHEED LONE el 8 de Mzo. de 2021
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 el 8 de Mzo. de 2021
repmat was discussed above. Bill was hoping for something more efficient.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by