How to replace k-th diagonal by vector?
    9 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    Clarisha Nijman
      
 il 2 Ott 2018
  
    
    
    
    
    Risposto: Clarisha Nijman
      
 il 3 Ott 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?
0 Commenti
Risposta accettata
  Guillaume
      
      
 il 2 Ott 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 Commenti
  Guillaume
      
      
 il 3 Ott 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
Più risposte (3)
  Bruno Luong
      
      
 il 3 Ott 2018
        
      Modificato: Bruno Luong
      
      
 il 3 Ott 2018
  
      >> 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
>>
0 Commenti
  Adam
      
      
 il 2 Ott 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 Commenti
  Guillaume
      
      
 il 2 Ott 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.
Vedere anche
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!