Vandermonde-like matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mokrane Mahdi
el 3 de Mzo. de 2022
Comentada: Mokrane Mahdi
el 3 de Mzo. de 2022
For any given number x, what's the easiest way to generate the following square matrix without using any loop:
0 x^(-0.5) x^(-1.0) x^(-1.5) x^(-2.0)
x^(-0.5) x^(-1.0) x^(-1.5) x^(-2.0) x^(-2.5)
x^(-1.0) x^(-1.5) x^(-2.0) x^(-2.5) x^(-3.0)
x^(-1.5) x^(-2.0) x^(-2.5) x^(-3.0) x^(-3.5)
x^(-2.0) x^(-2.5) x^(-3.0) x^(-3.5) x^(-4.0)
Here, in this example, I set the size of the matrix to be 5, but it has to be generated for any integer n.
Notice the matrix does look like the Vandermonde matrix, hence the idea of using repmat and cumprod commands..
Thanks in advance !
0 comentarios
Respuesta aceptada
KSSV
el 3 de Mzo. de 2022
p = -(0:0.5:4) ; % power values
n = 5 ; % n value
ind1 = bsxfun(@plus, (1 : n), (0 : numel(p) - n).'); % make moving window indices
p = p(ind1) % power values
x = rand ; % your x
V = x.^p % what you wanted
Más respuestas (1)
John D'Errico
el 3 de Mzo. de 2022
The easiest way? Probably this line: (in R2016b or later)
x.^(((0:-1:1-n)' + (0:-1:1-n))/2)
If you want to use two lines of code, then it looks simpler yet.
N = (0:-1:1-n)/2;
x.^(N' + N)
Easrlier releases than R2016b would use bsxfun.
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!