113 views

Skip to first unread message

Nov 20, 2007, 3:50:52 AM11/20/07

to

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.

Regards,

Gregory

Nov 21, 2007, 2:41:48 AM11/21/07

to

Your example below confuses your question, because if x={2,7} then x

[[1]==2 && x[[2]]==7. I recall an earlier post of yours, however, in

which you wanted lists of one element to be replaced by that element.

The following simple rule does this

[[1]==2 && x[[2]]==7. I recall an earlier post of yours, however, in

which you wanted lists of one element to be replaced by that element.

The following simple rule does this

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.

Nov 21, 2007, 2:42:50 AM11/21/07

to

On Nov 20, 2007, at 3:46 AM, Gregory Lypny wrote:

> 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

Nov 21, 2007, 2:46:54 AM11/21/07

to

Yes, I'm trying to reproduce the {.} problem with some regularity.

Be that as it may, it's not clear why, when I iterate over {i,

1,...,8} and {j ,1,...,8}, I get tables with dimensions {8, 8, 1}

rather than {8, 8}. That's the cut-and-paste problem. Sigh.

Be that as it may, it's not clear why, when I iterate over {i,

1,...,8} and {j ,1,...,8}, I get tables with dimensions {8, 8, 1}

rather than {8, 8}. That's the cut-and-paste problem. Sigh.

Gregory

Nov 21, 2007, 2:47:56 AM11/21/07

to

Gregory Lypny wrote:

> 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

Nov 21, 2007, 2:50:00 AM11/21/07

to

Gregory Lypny wrote:

> 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]]?

> 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]]?

x[[1]] *does* return 2 and not {2}, and x[[2]] *does* return 7.

--

Helen Read

University of Vermont

Nov 21, 2007, 2:54:04 AM11/21/07

to

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

Nov 21, 2007, 3:00:10 AM11/21/07

to

I don't get {2} when I calculate {2,7}[[1]], I get just 2, and this is

the documented behaviour. Could it be that the tables you have

constructed are not quite as you think? Try FullForm if you are not sure.

the documented behaviour. Could it be that the tables you have

constructed are not quite as you think? Try FullForm if you are not sure.

Matthew.

In[7]:= x = {2, 7}

x[[1]]

Out[7]= {2, 7}

Out[8]= 2

Nov 21, 2007, 3:18:29 AM11/21/07

to

On 11/20/07 at 3:46 AM, gregor...@videotron.ca (Gregory Lypny)

wrote:

wrote:

>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

Nov 21, 2007, 3:31:46 AM11/21/07

to

Hi,

with:

x={2,7}

you get already

for x[[1]] 2 and for x[[2]] 7 and *not*

{1} or {7} ...

Regards

Jens

Nov 21, 2007, 6:05:20 AM11/21/07

to

Gregory Lypny wrote:

> Yes, I'm trying to reproduce the {.} problem with some regularity.

> Be that as it may, it's not clear why, when I iterate over {i,

> 1,...,8} and {j ,1,...,8}, I get tables with dimensions {8, 8, 1}

> rather than {8, 8}. That's the cut-and-paste problem. Sigh.

>

> Yes, I'm trying to reproduce the {.} problem with some regularity.

> Be that as it may, it's not clear why, when I iterate over {i,

> 1,...,8} and {j ,1,...,8}, I get tables with dimensions {8, 8, 1}

> rather than {8, 8}. That's the cut-and-paste problem. Sigh.

>

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

Nov 22, 2007, 4:51:59 AM11/22/07

to

Thanks everyone for your insights,

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

Nov 23, 2007, 5:30:15 AM11/23/07

to

Gregory Lypny wrote:

> Thanks everyone for your insights,

>

> 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,

> Thanks everyone for your insights,

>

> 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,

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

Reply all

Reply to author

Forward

0 new messages

Search

Clear search

Close search

Google apps

Main menu