No BSD License  

Highlights from
diagonal

1.0

1.0 | 4 ratings Rate this file 4 Downloads (last 30 days) File Size: 241 Bytes File ID: #10324

diagonal

by Miguel D. B.

 

11 Mar 2006 (Updated 14 Mar 2006)

Returns a vector with the main diagonal of a matrix. Similar to the built-in function diag

| Watch this File

File Information
Description

This file contians a function named "diagonal01" which isolates in a row vector the diagonal of a matrix (square or not), whitout using a loop. It is similar to the Matlab built-in function "diag" when used with one input argument.
For example, the instructions
» A=magic(4); A(end,:)=[], B=diagonal01(A)

Produce

A =
    16 2 3 13
     5 11 10 8
     9 7 6 12
B =
    16 11 6

MATLAB release MATLAB 5.3 (R11)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
14 Mar 2006 Jos x

No need, it is exactly <diag> ...

14 Mar 2006 dave martin

Copying a Matlab built-in exactly := Mold on my sandwich.

15 Mar 2006 Vassili Pastushenko

"Returns a vector with the main diagonal of a matrix. Similar to the built-in function diag"

I would appreciate generalization of diag which would allow to replace matrix diagonals, not simply extracting diagonal vectors. Example of my "dream":

a=rand(3,5);
diag(a)=1;
diag(a)
ans =
1
1
1

15 Mar 2006 Jos x

Vassili, sometime dreams come true ...
a = ones(3,5)
a(~~eye(size(a))) = 99

15 Mar 2006 Vassili Pastushenko

Jos, I can easily write a faster version. Do you remember the discussion "how to delete the diagonal"? Jerome's addressing was the fastest (for sufficiently big matrices this is of importance). But I have so many own programs, that I put highest value to maximum possibilities with standard ML commands. What I "dreamed" of, was to add the addressing function to diag. Ideally it could look like

A(diag(A,K))= V(scalar or vector). I do not know whether it is possible. I can repeat only, that an m-file working like
A=putdiag(A,K,V) is no problem. Still I would appreciate it much higher that this diagonal.

As to me, I am not very stimulated to do new submissions, if they do not need some real efforts.

15 Mar 2006 Vassili Pastushenko

Jos, I can easily write a faster version. Do you remember the discussion "how to delete the diagonal"? Jerome's addressing was the fastest (for sufficiently big matrices this is of importance). But I have so many own programs, that I put a highest value to maximum possibilities with standard ML commands. What I "dreamed" of, was to add the addressing function to diag. Ideally it could look like

A(diag(A,K))= V(scalar or vector). I do not know whether it is possible. I can repeat only, that an m-file working like
A=putdiag(A,K,V) is not a problem. Still I would appreciate it much higher than this diagonal.

As to me, I am not very stimulated to do new submissions, if they do not need any real efforts.

15 Mar 2006 Vassili Pastushenko

Let me express better, what I mean. The present diag extracts values from matrix diagonals or builds matrices given a vector and diagonal index. These two functions are easily distinguished by input: vector--> build, matrix-->extract. I think it woulöd be more fundamental, if instead of extracting diagonal values, diag would produce indeces of those elements. Then I could both extract and replace diagonal values:

to extract (vector) V from diagonal K of matrix M:

V=M(diag(M,K)); (now V=diag(M,K))
to replace values:
M(diag(M,K))=V, V=scalar, vector or matrix(understood as a vector) (now does not work)

and the building command
M=diag(V,K) can work as before.

It is easy to write down such an m-file, but final decision whether this symmetrization will be introduced or not belongs to the architects of ML.

11 Oct 2007 Edmund Brekke

I do not understand the point of writing this function since it does nothing new. What I would be interested in is a function extracting the diagonal of a qadratic form x'*A*x where x is K x N and A is K x K, for K typically small and A typically small. Does anybody know how to do this without constructing the full matrix x'*A*x ?

11 Oct 2007 Edmund Brekke

Sorry, I meant "...for K typically small and N typically large".

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
linear algebra Miguel D. B. 22 Oct 2008 08:18:14
matrix Miguel D. B. 22 Oct 2008 08:18:14
diagonal Miguel D. B. 22 Oct 2008 08:18:14
diag Miguel D. B. 22 Oct 2008 08:18:14
mathematics Miguel D. B. 22 Oct 2008 08:18:14

Contact us at files@mathworks.com