Filter löschen
Filter löschen

How to replace k-th diagonal by vector?

28 Ansichten (letzte 30 Tage)
Clarisha Nijman
Clarisha Nijman am 2 Okt. 2018
Beantwortet: Clarisha Nijman am 3 Okt. 2018
I have a code here that give me errors. In the code I am computing a i-th dot product and want to replace this values on the i-th super diagonal.
lambda=0.42;
n=100;
p=0.005;
P=zeros(1001,1001);
for i=1:1000
a=i:1000;
b=a-i;
v=poisspdf(a,lambda);
w=binopdf(b,n,p);
c=dot(v,w);
d=size(diag(P,i),1);%this is the size of the vector with elements of the kth diagonal
e=c*ones(d,1);
diag(P,i)=e;
end
But matlab gives me an error saying:
Subscript indices must either be real positive integers or logicals.
Error in pdfnumber (line 18)
diag(P,i)=e;
Then I tried a more simple code. But is only replaces the 1 super diagonal for me, while i runs from 1 to 1000.
for i=1:1000
a=i:1000;
b=a-i;
v=poisspdf(a,lambda);
w=binopdf(b,n,p);
c=dot(v,w);
P(i,i+1)=c;
end
What am I doing wrong?

Akzeptierte Antwort

Guillaume
Guillaume am 2 Okt. 2018
You can't use diag to assign to diagonales of a matrix, only to get them. The simplest way to assign to the diagonales is to replace:
d=size(diag(P,i),1);%this is the size of the vector with elements of the kth diagonal
e=c*ones(d,1);
diag(P,i)=e;
by
P(i*size(P, 1)+1:size(P, 1)+1:end) = e;
which simply computes the linear indices of the diagonal elements.
  3 Kommentare
Clarisha Nijman
Clarisha Nijman am 3 Okt. 2018
Hallo,
The suggestion of Guillame works the best
P(i*size(P, 1)+1:size(P, 1)+1:end) = e;
But there is still something more:
It only replaces the super-diagonals. As I do not fully understand the code, I am trying a lot of other combination to replace also the sub-diagonals.
Things as: P(-i*size(P, 1)+1:size(P, 1)+1:end) = e; P(start:i*size(P, 1)+1:size(P, 1)+1) = e; P(begin:i*size(P, 1)+1:size(P, 1)+1) = e;
But alas! I did not succeed. Can you help me also with this?
kind regards,
Clarisha
Guillaume
Guillaume am 3 Okt. 2018
Well, your original code did not access the subdiagonals so I assumed that you were only interested in the super-diagonals.
For subdiagional -i, use:
P(i+1:size(P, 1)+1:1+size(P, 1)*min(size(P, 1)-i,size(P, 2))) = e; %where i is the positive index of subdiagonal -i. 0 is main diagonal

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Bruno Luong
Bruno Luong am 3 Okt. 2018
Bearbeitet: Bruno Luong am 3 Okt. 2018
You can use the function IDIAG in this FEX
>> P = rand(5,4)
P =
0.3517 0.2858 0.0759 0.1299
0.8308 0.7572 0.0540 0.5688
0.5853 0.7537 0.5308 0.4694
0.5497 0.3804 0.7792 0.0119
0.9172 0.5678 0.9340 0.3371
>> P(idiag(size(P),-1)) = (1:4)
P =
0.3517 0.2858 0.0759 0.1299
1.0000 0.7572 0.0540 0.5688
0.5853 2.0000 0.5308 0.4694
0.5497 0.3804 3.0000 0.0119
0.9172 0.5678 0.9340 4.0000
>>

Adam
Adam am 2 Okt. 2018
P is defined as a matrix of all zeros and you appear to be overwriting the 'diag' function with a variable of the same name which you then try to index into using P. The entirety of P, in fact!
  2 Kommentare
Clarisha Nijman
Clarisha Nijman am 2 Okt. 2018
Can you pls explain your answer a little bit more?
Guillaume
Guillaume am 2 Okt. 2018
You cannot assign a value to a function, so
somefunction(atsomevalue) = something
is never correct in matlab. What you are attempting to do is assign a new value to the return value of the diag function, so if we'd made that explicit, your
diag(P, i) = e;
could be decomposed to:
tempvariable = diag(P, i);
tempvariable = e;
Even if matlab allowed that, you can see it still wouldn't work as you would only be changing the value of the temporary, not the diagonale of P.
Since assigning a value to a function is not allowed and since matlab allows you to use the same names for functions and variables, matlab interpret your diag line as an assigment of e to the matrix diag at rows P and column i. Some values in P are 0 which is not a valid row index, hence the error.

Melden Sie sich an, um zu kommentieren.


Clarisha Nijman
Clarisha Nijman am 3 Okt. 2018
Yes Guillaume! It works! Thanks a lot. You are indeed right about the code. I pasted the code partially, and expected that the leading question: How to replace k-th diagonal by vector?, would cover the rest.
But thanks anyway!!!

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by