# How to index with a changing index

3 visualizaciones (últimos 30 días)
xiaojuezi el 8 de Dic. de 2021
Respondida: DGM el 8 de Dic. de 2021
Hi, I would like to construct a matrix L of size nxn that looks like below:
L = [a,b,c,0,0,0,0,...
0,a,b,c,0,0,0,...
0,0,a,b,c,0,0,...
0,0,0,a,b,c,0,...
...
0,0,0,0,...a,b,c];
I'm trying to do this without the for loop.
i = 2:n-1;
L(:,i-1:i+1) = repmat([a,b,c],n,1);
However, this doesn't give what I want, and I was warned by the complier that i is a non-scalar operator.
Is there a way to fix this?
Thank you very much!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (1)

DGM el 8 de Dic. de 2021
You can try something like this:
a = 1;
b = 2;
c = 3;
A = toeplitz([a 0 0 0 0 0 0],[a b c 0 0 0 0]);
A = A(1:end-2,:)
A = 5×7
1 2 3 0 0 0 0 0 1 2 3 0 0 0 0 0 1 2 3 0 0 0 0 0 1 2 3 0 0 0 0 0 1 2 3
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

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

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by