-3
$\begingroup$

I would like to build a vector with the values obtained from computation of the diagonal terms of the following Table:

 Table[Table[l + j, {l, 0, 10, 1}], {j, 0, 0.1, 0.01}] // MatrixForm

My real Table is much more complicated and time consuming, therefore I am looking for a fast way to avoid all computations of terms out of diagonal. Is there any function or way?

I would like to underline that my real Table has the following shape, which is forcing me to find a smart way to compute the diagonal terms:

vectorj=Table[j,{j, 0, 0.1, 0.01}]
f[x_]=Table[x^2*j,{j,vectorj}]   
Table[j*f[x][[j]],{j,1,Length[vectorj]}] // MatrixForm

The problem is that I need in the last table j to be an integer to get the [[j]] term of f[x], but the multypling factor j should be actually the value in the j position, say in position 2, that should be 0.01. I want to avoid building too many tables so I thought there might be a way to avoid useless computations.

$\endgroup$
3
$\begingroup$

To talk about a diagonal of the matrix it must be square. So in general make each of your two vectors (which are equal length due to squareness):

ivector=Table[i,{i,0,10,1}];
jvector=Table[j,{j,0,0.1,0.001}];

Then apply your matrix function f[i,j] to these two vectors as f[t,t] along the diagonal:

answer=Table[ivector[[t]]+jvector[[t]],{t,1,Length[ivector]}]
$\endgroup$
  • $\begingroup$ a couple of things: first, isn't there syntax error in the definition of ivector and jvector? secondly, i don't get anything after running your code $\endgroup$ – Andrea G May 24 '17 at 9:56
  • 1
    $\begingroup$ Yes there was a typo in my two vectors. This is now fixed. This error was what was causing there to be no output. $\endgroup$ – Ian Miller May 24 '17 at 10:25
2
$\begingroup$

First, I'd rewrite your code above

Table[Table[l + j, {l, 0, 10, 1}], {j, 0, 0.1, 0.01}] // MatrixForm

into

Table[l + j/100, {j, 0, 10}, {l, 0, 10}] // MatrixForm

Now the indices are comparable, and you can just write the following to get the diagonal elements:

Table[j + j/100, {j, 0, 10}]

In general, if you are making j do double duty as both an argument to a function and an index to a table, better to treat it fundamentally as an integer index and compute the argument.

$\endgroup$
  • $\begingroup$ ok this is fine if we can make the indices comparable. In this case we were lucky because of that 1/100 factor. And in a general case where this is not possible? $\endgroup$ – Andrea G May 23 '17 at 15:13
  • $\begingroup$ I think it will always be possible to rewrite into an integer index form, since it is just a linear transformation from the original sequence to the sequence {0,1,2,...}. Can you think of a counter-example? $\endgroup$ – MikeY May 23 '17 at 15:17
  • $\begingroup$ Values that I want the function to assume: 0, 0.1, 0.6, 100, 1023.5; Indices of the vector: 1, 2, 3, 4, 5. What do you think of such a case? $\endgroup$ – Andrea G May 23 '17 at 15:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.