Say I have the list x={2, 7}. Is there a command I can use to get 2
rather than {2} when I ask for x[[1]], and 7 rather than {7} when I
ask for x[[2]]? Doing so would solve my problem with my tables being
too deep to be able to cut and paste directly into the table objects
of other applications like word processors and spread sheets.
Regards,
Gregory
r1={e_}:>e
For example,
In[265]:= {{3,4},{2},{1,2},{3}}/.r1
Out[265]= {{3,4},2,{1,2},3}
This simple rule and application can be tweaked to avoid failures in
special cases. Read the docs.
> Hello everyone,
>
> Say I have the list x={2, 7}. Is there a command I can use to get 2
> rather than {2} when I ask for x[[1]], and 7 rather than {7} when I
> ask for x[[2]]? Doing so would solve my problem with my tables being
> too deep to be able to cut and paste directly into the table objects
> of other applications like word processors and spread sheets.
x[[1]] is 2 and x[[2]] is 7. x[[{1}]] is {2} and x[[{2}]] is {7}.
Regards,
Ssezi
Gregory
> Say I have the list x={2, 7}. Is there a command I can use to get 2
> rather than {2} when I ask for x[[1]], and 7 rather than {7} when I
Here, I do not follow (or understand) you. You have a list of scalars
and taking any one of its elements with *Part* will produce a scalar and
not a list of one element. Or you may have something else in mind... For
instance,
In[1]:= x = {2, 7}
Out[1]= {2, 7}
In[2]:= x[[1]]
Out[2]= 2
In[3]:= x[[2]]
Out[3]= 7
As you can see above, no list is returned in this case.
> ask for x[[2]]? Doing so would solve my problem with my tables being
> too deep to be able to cut and paste directly into the table objects
> of other applications like word processors and spread sheets.
You could use the system variable *$Post* and set up your own post
processing function. (You might be interested in $PrePrint too.) Here is
an example of such a function that should do what you are looking for,
though you may want to add some additional tests (or remove few of them).
In[1]:= $Post =
If[Head[#] === List && Length[#] == 1 &&
Depth[#] < 3, #[[1]], #] &;
prob = {.4, {.6}, {.4, .6}, {{.4, .6}}};
prob[[1]] (* Scalar: not a list, depth one *)
prob[[2]] (* List of length one and depth two *)
prob[[3]] (* List of length two and depth two *)
prob[[4]] (* List of length one and depth three *)
$Post =. (* Reset $Post *)
Out[3]= 0.4
Out[4]= 0.6
Out[5]= {0.4, 0.6}
Out[6]= {{0.4, 0.6}}
Best regards,
--
Jean-Marc
x[[1]] *does* return 2 and not {2}, and x[[2]] *does* return 7.
--
Helen Read
University of Vermont
I am not sure what you mean, since
In[1]:= x={2,7}
Out[1]= {2,7}
In[2]:= x[[1]]
Out[2]= 2
--
Szabolcs
Matthew.
In[7]:= x = {2, 7}
x[[1]]
Out[7]= {2, 7}
Out[8]= 2
>Say I have the list x={2, 7}. Is there a command I can use to get 2
>rather than {2} when I ask for x[[1]], and 7 rather than {7} when I
>ask for x[[2]]? Doing so would solve my problem with my tables
>being too deep to be able to cut and paste directly into the table
>objects of other applications like word processors and spread sheets.
Your wording is very confusing or perhaps you haven't stated
your problem quite right. When you say you "ask for x[[1]]" what
does that mean if not entering x[[1]] somewhere? And if that is
what you mean, then since
In[14]:= x = {2, 7};
In[15]:= x[[1]]
Out[15]= 2
then there is something wrong with what you wrote above.
Generally, it is better to post some code to eliminate this kind
of confusion.
--
To reply via email subtract one hundred and four
with:
x={2,7}
you get already
for x[[1]] 2 and for x[[2]] 7 and *not*
{1} or {7} ...
Regards
Jens
Gregory,
Table[i*j, {i, 8}, {j, 8}] returns a list with dimensions {8, 8}. But
if the expression inside Table returns a single-element list, then the
table will have dimensions {8, 8, 1}. Example: Table[{i*j}, {i, 8}, {j,
8}]. So you could try using
Table[
First[(complicated expression that returns a single element list)],
{i, 8}, {j, 8}
]
Szabolcs
I've found the problem. Say you want to sum a list whose elements are
13 and 9. Mathematica will return a list with one element, {22},
rather than 22 if the original list is specified as a 2x1 column vector.
x = {{13}, {9}}; y = Total@x >>> returns {22}
This also happens if you write x as I have above, or you use
Mathematica's Insert menu to create a more visually appealing column
vector, but it does not happen if you define x as a 2x1 array using
the Array command.
I think it will happen with any matrix calculation whose result should
otherwise be a scalar. If we now let y be the row vector {1, 1} then
y.x >>> returns {22}
The upshot of this is that any table that is created from calculations
that make use of column vectors or matrix math will likely have a
depth greater than 3, and you won't be able to cut and paste directly
into a word processor or spreadsheet.
I'm going to have a look at some of the work-arounds that have been
suggested in this thread and my related thread "Copy and Pasting
Tables into Spreadsheet".
Gregory
Tip: Use CTRL+Enter and CTRL+, instead of the insert menu.
> but it does not happen if you define x as a 2x1 array using
> the Array command.
Yes, it does:
In[1]:= Array[# &, {2, 1}]
Out[1]= {{1}, {2}}
In[2]:= Total[%]
Out[2]= {3}
>
> I think it will happen with any matrix calculation whose result should
> otherwise be a scalar. If we now let y be the row vector {1, 1} then
>
> y.x >>> returns {22}
>
This is because Mathematica works with tensors of arbitrary dimensions,
not just matrices or vectors. So in Mathematica it does not make sense
to speak about column or row vectors---all vectors (1D arrays) are
treated in the same way. {{1},{2}} is a 2 by 1 matrix and {{1, 2}} is a
1 by 2 matrix, not vectors. (Of course all this is just a question of
naming conventions. I am just trying to explain why Mathematica works
this way.)
> The upshot of this is that any table that is created from calculations
> that make use of column vectors or matrix math will likely have a
> depth greater than 3, and you won't be able to cut and paste directly
> into a word processor or spreadsheet.
When you multiply a vector with a matrix, you get a vector. If you
multiply a vector with a vector, you get a scalar:
In[4]:= {x, y}.{{a, b}, {c, d}}
Out[4]= {a x + c y, b x + d y}
In[5]:= {{a, b}, {c, d}}.{x, y}
Out[5]= {a x + b y, c x + d y}
In[6]:= {x, y}.{a, b}
Out[6]= a x + b y
If the distinction between column and row vectors is important, just
work with matrices, and you'll always get matrices as the result (no
scalars):
In[7]:= {{x, y}}.{{a}, {b}}
Out[7]= {{a x + b y}}
In[8]:= {{a}, {b}}.{{x, y}}
Out[8]= {{a x, a y}, {b x, b y}}
If it is not important to differentiate between row and column vectors,
then use 1D arrays to represent vectors. The Outer product can be
calculated like this:
In[9]:= Outer[Times, {a, b}, {x, y}]
Out[9]= {{a x, a y}, {b x, b y}}
>
> I'm going to have a look at some of the work-arounds that have been
> suggested in this thread and my related thread "Copy and Pasting
> Tables into Spreadsheet".
>
>
> Gregory
>
--
Szabolcs