Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

Subject: Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

From: Franz

Date: 18 Mar, 2013 18:40:12

Message: 1 of 8

I was wondering whether there is any procedure equivalent for the 'Table' command in Mathematica for creating multidimensional arrays (tensors) from given vectors/arrays?

In my specific example -although I would be interested in a general case- I have two vectors and I want to create a matrix out of them without forming an outer product.

I guess it will be clearer what I want once I give example codes:
define two vectors :
x=[0 1 3] ;
y=[2 5 6 0] ;
In Mathematica I could now do the following to create a matrix:
z=Table{x[[iterator]]+y[[kterator]],{iterator,1,3},{kterator,1,4}}
This would create a 3x4 matrix where the (i,k)-th element is found by performing the wanted operation on the i-th element of x and k-th element of y.

Now I know that Matlab is supposed to be almighty when it comes to matrices, but the only way of doing something equivalent to the above Table command I can see at the moment is doing two nested for-loops:
z=zeros(3,4);
for i=1:3
for k=1:4
z(i,k)=x(i)+y(k);
end
end

My question now: is there any procedure in Matlab I can use instead of nested for loops that is equivalent to the "Table" command in Mathematica?

Subject: Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

From: Franz

Date: 18 Mar, 2013 19:19:10

Message: 2 of 8

I have just thought of the following procedure myself, although I believe that there must be something more elegant than this for a general case:
x2=transpose(ones(4,1)*x);
y2=ones(3,1)*y;
z2=x2+y2;

Subject: Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

From: Josh Meyer

Date: 18 Mar, 2013 19:41:47

Message: 3 of 8

slightly better with only one loop:

for i=1:length(x)
z(i,:) = bsxfun(@plus,x(i),y)
end

"Franz " <lang.franz88@gmail.com> wrote in message
news:ki7n2c$2gd$1@newscl01ah.mathworks.com...
> I was wondering whether there is any procedure equivalent for the 'Table'
> command in Mathematica for creating multidimensional arrays (tensors) from
> given vectors/arrays?
>
> In my specific example -although I would be interested in a general case-
> I have two vectors and I want to create a matrix out of them without
> forming an outer product.
> I guess it will be clearer what I want once I give example codes:
> define two vectors :
> x=[0 1 3] ;
> y=[2 5 6 0] ;
> In Mathematica I could now do the following to create a matrix:
> z=Table{x[[iterator]]+y[[kterator]],{iterator,1,3},{kterator,1,4}}
> This would create a 3x4 matrix where the (i,k)-th element is found by
> performing the wanted operation on the i-th element of x and k-th element
> of y.
>
> Now I know that Matlab is supposed to be almighty when it comes to
> matrices, but the only way of doing something equivalent to the above
> Table command I can see at the moment is doing two nested for-loops:
> z=zeros(3,4);
> for i=1:3
> for k=1:4
> z(i,k)=x(i)+y(k);
> end
> end
>
> My question now: is there any procedure in Matlab I can use instead of
> nested for loops that is equivalent to the "Table" command in
> Mathematica?

Subject: Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

From: Josh Meyer

Date: 18 Mar, 2013 20:03:21

Message: 4 of 8

Silly me, bsxfun has singleton expansion enabled already! This is what
you're looking for:

z = bsxfun(@plus,x',y)

"Josh Meyer" <jmeyer@mathworks.com> wrote in message
news:ki7qlt$ecs$1@newscl01ah.mathworks.com...
> slightly better with only one loop:
>
> for i=1:length(x)
> z(i,:) = bsxfun(@plus,x(i),y)
> end
>
> "Franz " <lang.franz88@gmail.com> wrote in message
> news:ki7n2c$2gd$1@newscl01ah.mathworks.com...
>> I was wondering whether there is any procedure equivalent for the 'Table'
>> command in Mathematica for creating multidimensional arrays (tensors)
>> from given vectors/arrays?
>>
>> In my specific example -although I would be interested in a general
>> case- I have two vectors and I want to create a matrix out of them
>> without forming an outer product.
>> I guess it will be clearer what I want once I give example codes:
>> define two vectors :
>> x=[0 1 3] ;
>> y=[2 5 6 0] ;
>> In Mathematica I could now do the following to create a matrix:
>> z=Table{x[[iterator]]+y[[kterator]],{iterator,1,3},{kterator,1,4}}
>> This would create a 3x4 matrix where the (i,k)-th element is found by
>> performing the wanted operation on the i-th element of x and k-th element
>> of y.
>>
>> Now I know that Matlab is supposed to be almighty when it comes to
>> matrices, but the only way of doing something equivalent to the above
>> Table command I can see at the moment is doing two nested for-loops:
>> z=zeros(3,4);
>> for i=1:3
>> for k=1:4
>> z(i,k)=x(i)+y(k);
>> end
>> end
>>
>> My question now: is there any procedure in Matlab I can use instead of
>> nested for loops that is equivalent to the "Table" command in
>> Mathematica?
>

Subject: Matlab procedure equivalent to 'table" command in Mathematica

From: Nasser M. Abbasi

Date: 19 Mar, 2013 02:21:16

Message: 5 of 8

On 3/18/2013 2:03 PM, Josh Meyer wrote:
> Silly me, bsxfun has singleton expansion enabled already! This is what
> you're looking for:
>
> z = bsxfun(@plus,x',y)
>

This question really make no sense to me.

OP said

"I have two vectors and I want to create a matrix out of them
without forming an outer product"

I have no idea why OP did not want to use outer product in Mathematica,
Becuase the above solution in Matlab is effectively the outer product.

So, I do not understand. Why not use it Mathematica, but ok to use
itin Matlab?

In M one can do

Outer[Plus, x, y]

and get the same answer as bsxfun() command.

--Nasser

Subject: Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

From: Franz

Date: 19 Mar, 2013 08:36:15

Message: 6 of 8

Thanks, bsxfun in this case does the job. Nevertheless, I think it is still very restrictive in what functions you can use with it, and also generalisations to higher dimensions (say creating a m X n X p array out of a m X n matrix and a vector with dimension p) seem generally not possible. But thanks for the help!

"Josh Meyer" <jmeyer@mathworks.com> wrote in message <ki7rub$inb$1@newscl01ah.mathworks.com>...
> Silly me, bsxfun has singleton expansion enabled already! This is what
> you're looking for:
>
> z = bsxfun(@plus,x',y)
>
> "Josh Meyer" <jmeyer@mathworks.com> wrote in message
> news:ki7qlt$ecs$1@newscl01ah.mathworks.com...
> > slightly better with only one loop:
> >
> > for i=1:length(x)
> > z(i,:) = bsxfun(@plus,x(i),y)
> > end
> >

Subject: Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

From: Duncan Po

Date: 19 Mar, 2013 12:37:07

Message: 7 of 8

bsxfun works for higher dimensions. It can operate on a m X n matrix and a 1 X 1 X p vector to generate a m X n X p array.
Also, it works for any element-wise functions (functions that operate on individual elements)

"Franz " <lang.franz88@gmail.com> wrote in message <ki981u$jhn$1@newscl01ah.mathworks.com>...
> Thanks, bsxfun in this case does the job. Nevertheless, I think it is still very restrictive in what functions you can use with it, and also generalisations to higher dimensions (say creating a m X n X p array out of a m X n matrix and a vector with dimension p) seem generally not possible. But thanks for the help!
>
> "Josh Meyer" <jmeyer@mathworks.com> wrote in message <ki7rub$inb$1@newscl01ah.mathworks.com>...
> > Silly me, bsxfun has singleton expansion enabled already! This is what
> > you're looking for:
> >
> > z = bsxfun(@plus,x',y)
> >
> > "Josh Meyer" <jmeyer@mathworks.com> wrote in message
> > news:ki7qlt$ecs$1@newscl01ah.mathworks.com...
> > > slightly better with only one loop:
> > >
> > > for i=1:length(x)
> > > z(i,:) = bsxfun(@plus,x(i),y)
> > > end
> > >

Subject: Matlab procedure equivalent to 'table" command in Mathematica for creating arrays

From: Steven_Lord

Date: 19 Mar, 2013 13:45:53

Message: 8 of 8



"Duncan Po" <Duncan.Po@mathworks.com> wrote in message
news:ki9m5j$rka$1@newscl01ah.mathworks.com...
> bsxfun works for higher dimensions. It can operate on a m X n matrix and a
> 1 X 1 X p vector to generate a m X n X p array. Also, it works for any
> element-wise functions (functions that operate on individual elements)
>
> "Franz " <lang.franz88@gmail.com> wrote in message
> <ki981u$jhn$1@newscl01ah.mathworks.com>...
>> Thanks, bsxfun in this case does the job. Nevertheless, I think it is
>> still very restrictive in what functions you can use with it, and also
>> generalisations to higher dimensions (say creating a m X n X p array out
>> of a m X n matrix and a vector with dimension p) seem generally not
>> possible. But thanks for the help!

The table in the documentation for BSXFUN:

http://www.mathworks.com/help/matlab/ref/bsxfun.html

is a (non-exhaustive) list describing some of the built-in mathematical
functions that work with BSXFUN. The paragraph immediately below the table
reads:

"fun can also be a handle to any binary element-wise function not listed
above. A binary element-wise function of the form C = fun(A,B) accepts
arrays A and B of arbitrary but equal size and returns output of the same
size. Each element in the output array C is the result of an operation on
the corresponding elements of A and B only. fun must also support scalar
expansion, such that if A or B is a scalar, C is the result of applying the
scalar to every element in the other input array."

So as Duncan said, if you want to use BSXFUN with an arbitrary element-wise
function you can:

x = 1:10;
y = reshape(1:5, 1, 1, 5);
fun = @(a, b) a.*b.^3; % This is an element-wise function
z = bsxfun(fun, x, y)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us