### Tags: array, cell, containing, matlab, programming, row, simpledata, size, square, value, variable, vector, vectors

# Square each vector in a cell array

On Programmer » Matlab

2,692 words with 3 Comments; publish: Fri, 02 May 2008 00:53:00 GMT; (20077.88, « »)

Hi,

I have a cell array containing variable size of row vectors in each cell.

I'd like to square each value of each vector.

A simple

data(13,:).^2 is not working.

I thought about something with cellfun and power(data,2), but I am not famil

iar with cellfun. Perhaps somebody had a similar problem before?

Okay, I could create a for loop, but I'd like to avoid for loops.

Kind regards,

Gudrun

*http://matlab.todaysummary.com/q_matlab_57102.html*

All Comments

Leave a comment...

- 3 Comments
- "bluesaturn[at]kellnerweg.de" <bluesaturn.matlab.todaysummary.com.kellnerweg.de>
wrote in message

<10726401.1202827559569.JavaMail.jakarta.matlab.todaysummary.com.nitrogen.mathforum.org>...

> Hi,

> I have a cell array containing variable size of row

vectors in each cell.

> I'd like to square each value of each vector.

> A simple

> data(13,:).^2 is not working.

> I thought about something with cellfun and power(data,2),

but I am not familiar with cellfun. Perhaps somebody had a

similar problem before?

> Okay, I could create a for loop, but I'd like to avoid

for loops.

> Kind regards,

> Gudrun

with cell arrays, use {} to index them:

ans =

Columns 1 through 7

0.9423 0.3418 0.4018 0.3077 0.4116

0.2859 0.3941

Columns 8 through 10

0.5030 0.7220 0.3062

ans =

Columns 1 through 7

0.8880 0.1168 0.1614 0.0947 0.1694

0.0818 0.1553

Columns 8 through 10

0.2530 0.5213 0.0938

#1; Fri, 02 May 2008 00:54:00 GMT

- "bluesaturn[at]kellnerweg.de" <bluesaturn.matlab.todaysummary.com.kellnerweg.de>
- On Tue, 12 Feb 2008 09:45:29 -0500, bluesaturn[at]kellnerweg.de wrote:
> Hi,

> I have a cell array containing variable size of row vectors in each cell.

> I'd like to square each value of each vector.

> A simple

> data(13,:).^2 is not working.

>

a={rand(3) rand(4) rand(5)}

cellfun(.matlab.todaysummary.com.(x) x.^2, a, 'UniformOutput',false)

Alessandro Mura

INAF - Istituto Nazionale di Astrofisica, Roma, Italy

http://pptt4.ifsi-roma.inaf.it/~mura/index.html

#2; Fri, 02 May 2008 00:55:00 GMT

- On Tue, 12 Feb 2008 09:45:29 -0500, bluesaturn[at]kellnerweg.de wrote:
- >> data{13}=data{13}.^2;
This squares only one cell. But what do you do if you have cell array like t

hat?

data{13,1}=rand(1,10);

data{13,2}=rand(1,10);

And I like to do something like data(13,:).^2, so I would square data{13,1}

and data{13,2} both.

I know data(13,:).^2 is not working.

Sorry, if I misunderstood you.

Kind regards,

Gudrun

#3; Fri, 02 May 2008 00:57:00 GMT

- >> data{13}=data{13}.^2;