0
$\begingroup$

I have an issue making an array by for. The idea is to generate the diagonal of the array by a progression, and then replicate that value for the lower part of the array. The part that makes the diagonal works just fine, but the part that replicate the diagonal value for 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=6;
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, ty

$\endgroup$
  • 1
    $\begingroup$ if is not a Mathematica command; use If. Moreover it is unclear to me what you want. Probably you mix up a[i,j] and A[[i,j]]. Only the latter indexes into an array. $\endgroup$ – Henrik Schumacher Mar 24 '18 at 21:08
  • 1
    $\begingroup$ To add onto what @HenrikSchumacher said unless it's inside Compile using For is probably not the best way to approach your problem $\endgroup$ – b3m2a1 Mar 24 '18 at 21:23
  • $\begingroup$ Yeah, Do is in many ways more robust, faster, and more concise than For. $\endgroup$ – Henrik Schumacher Mar 24 '18 at 21:40
  • $\begingroup$ Thanks for the feedback. I changed the If, but the strange behavior persist. there is no issue with the a[i,j] and A[[i,j]] , if that were the case it would fail at every iteration not just at the n-2. $\endgroup$ – Rodrigo Mar 25 '18 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.