How to expand a row of values into a matrix, such that each new value is equal to the value above it plus one?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marco
el 27 de Jun. de 2024 a las 16:57
Respondida: Umar
el 27 de Jun. de 2024 a las 17:03
What I'm asking for is a lot simpler to show than to explain. If I have a vector such as, for example:
[5 10 15]
How can I expand the vector for any integer length n (for this example let's say n=3) into a matrix such that:
[5 10 15; 6 11 16; 7 12 17]
I know there's a brute force solution with for-loops, but I'm trying to avoid for-loops where I can and stick to vectorization for efficiency. Is there anything I can use?
0 comentarios
Respuesta aceptada
Más respuestas (1)
Umar
el 27 de Jun. de 2024 a las 17:03
Hi Marco,
By using bsxfun with element-wise addition and transpose to adjust dimensions, you can efficiently expand the vector into a matrix without resorting to for-loops, ensuring a more vectorized and optimized solution.
For more information regarding bsxfun, please refer to
https://www.mathworks.com/help/matlab/ref/bsxfun.html
Hope that answers your question.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!