Re: Initialize a Diagonal Matrix

<michael@xxxxxxxxxxxxxxxx> wrote:

Other than using a DO loop is there any compact way of initializing the
elements of a diagonal matrix. For a vector one can write,
v(:)=(/1,3,10/). Is there something similar for a diagonal matrix?

Mostly, no. I think I might have seen an obscure related trick but I
don't recall the details and I might even misrecall having seen such a
thing. If it existed, it certainly wasn't anything that you'd actually
want to do other than to win a bet or some such thing.

I'm slightly puzzled in two ways about your mention of DO loops.

First, I feel obligated to ask what is wrong with DO loops. I ask
becasue there are some people who seem to have what I can only regard as
a prejuice against DO loops, thinking that they are somehow
intrinsically inferior to newer features of the language. I disagree
with that.

That aside, I have trouble imagining exactly what you are talking about
in DO loops. I don't know of a simple direct way to do this using DO
loops either. If I had a very small (say 3x3) array, I'd probably just
set it to 0 and then individually assign each of the diagonal elements.
If I had an array that was much larger than that ·or was dynamically
sized), I'd likely initialize a temporary rank 1 array for the diagnonal
elements and then use a DO loop to copy that to the diagonal of the
rank-2 array. Perhaps you meant something like that, but I wasn't quite
sure whether you had something else in mind, since I would say that the
main characterization of that method is that it uses a temporary rank-1
array for the diagonal; the DO loop seems like a smaller detail.

Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain