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
Could anyone help me please?
Thank you!

답변 (3개)

Simon
Simon 2013년 12월 12일

0 개 추천

Hi!
diag(q)

댓글 수: 2

FULYA  SEREF
FULYA SEREF 2013년 12월 12일
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).
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.

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2013년 12월 12일

0 개 추천

diag(q(1,2,3))
And for more info:
doc diag

댓글 수: 1

FULYA  SEREF
FULYA SEREF 2013년 12월 12일
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).

댓글을 달려면 로그인하십시오.

Jos (10584)
Jos (10584) 2013년 12월 13일

0 개 추천

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)

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

질문:

2013년 12월 12일

답변:

2013년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by