# creating a diagonal matrix?

2 visualizzazioni (ultimi 30 giorni)
FULYA SEREF il 12 Dic 2013
Risposto: Jos (10584) il 13 Dic 2013
Hi,
I want to create a diagonal matrix which has the elements like this: q1,q1,q2,q2,...,qm,qm
q is a function and has m different values for m different x-values
Thank you!
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (3)

Simon il 12 Dic 2013
Hi!
diag(q)
##### 2 CommentiMostra NessunoNascondi Nessuno
FULYA SEREF il 12 Dic 2013
thank you very much but as you see I have the elements who are the same for two times. I suppose I need a loop. I can't enter these values by hand. Because first I will define the function q. I need m different values of q for m different x values. And I have to create a matrix whose diagonal is for example; q(1),q(1),q(2),q(2), ,....,q(m),q(m).
Simon il 13 Dic 2013
So, if you have your function q, what will be the output? Is it a m-element vector? Or just a scalar value?
If you have your m-element vector X, you can do
ind = 1:m;
ind = [ind; ind];
diag(X(ind(:)))
If you get scalars only you have to create it in a loop.

Accedi per commentare.

Sean de Wolski il 12 Dic 2013
diag(q(1,2,3))
doc diag
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
FULYA SEREF il 12 Dic 2013
thank you very much but as you see I have the elements who are the same for two times. I suppose I need a loop. I can't enter these values by hand. Because first I will define the function q. I need m different values of q for m different x values. And I have to create a matrix whose diagonal is for example; q(1),q(1),q(2),q(2), ,....,q(m),q(m).

Accedi per commentare.

Jos (10584) il 13 Dic 2013
Like this?
q = [10 20 30] ;
q = q(floor(1:.5:numel(q)+.5)) % expand (there are many other ways to do this!)
diag(q)
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Operating on Diagonal Matrices in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by