# How to expand a row of values into a matrix, such that each new value is equal to the value above it plus one?

2 vues (au cours des 30 derniers jours)
Marco le 27 Juin 2024
Réponse apportée : Umar le 27 Juin 2024
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]
ans = 1x3
5 10 15
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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]
ans = 3x3
5 10 15 6 11 16 7 12 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Voss le 27 Juin 2024
v = [5 10 15];
n = 3;
m = v+(0:n-1).'
m = 3x3
5 10 15 6 11 16 7 12 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Plus de réponses (1)

Umar le 27 Juin 2024
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.
https://www.mathworks.com/help/matlab/ref/bsxfun.html
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by