Message Boards Message Boards

0
|
2012 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Perform recursive array generation?

Posted 6 years ago

Hello,

I'm trying to fill an array by for. The idea is to generate the diagonal of the array by a progression, and then replicate each element at (i,i) to the remnants rows below it . The part that makes the diagonal works just fine (from the first for to the end of the second for), but the part that replicate the diagonal value (the If line) to the lower part of the array messed up at column n-2 (it's always the n-2 column that fails). At this point I'm at lost of what is failing.

Here is the code:

Clear["Global`*"]
n=4;
V=Array[v,n+1,0];
v[0]=0;
A=Array[a, {n, n}];
For[i=1,i<=n,i++,
    a[i,i]=V[[i+1]];
    For[j=1, j<=i,j++,
       a[i,i]=Append[{a[i, i]}, j*p+V[[1+i-j]]];
    ];
If[i<n, A[[i + 1 ;; n, i]] = a[i, i]];
]
MatrixForm[A]

Any feedback is welcome. Thanks.

POSTED BY: Richard Peña
2 Replies

Is this what you are trying to do?

In[1]:= n = 4;

In[6]:= res = Table[j*Boole[i >= j], {i, n}, {j, n}]

Out[6]= {{1, 0, 0, 0}, {1, 2, 0, 0}, {1, 2, 3, 0}, {1, 2, 3, 4}}

enter image description here

POSTED BY: Frank Kampas

Is this what you are trying to do?

In[1]:= n = 4;

In[6]:= res = Table[j*Boole[i >= j], {i, n}, {j, n}]

Out[6]= {{1, 0, 0, 0}, {1, 2, 0, 0}, {1, 2, 3, 0}, {1, 2, 3, 4}}

enter image description here

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract