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:


A=Array[a, {n, n}];
    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]]; 

Any feedback is welcome, ty

  • 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.