Thread Subject: Basic Matlab Question

Subject: Basic Matlab Question

From: James Carter

Date: 16 Nov, 2009 23:46:01

Message: 1 of 13

I have a 32 x 1300 2D array, lets call it S. I know I can pick off a particular row with the following argument: A = S(1,:); which gives me a 1 x 1300 array from the first
row of the 2D array. I want every row assigned to a new indexed array, so I would start off with

for n = 1:rows
A(n) = S(n,:);
end

This doesn't work because of a dimensional mismatch. I'm not really trying to define
a dimension but simply declare a unique identifier. How am I able to assign rows
such that I can put them in A(1), A(2), etc., all with the dimension 1 x 1300.

Thanks!

Subject: Basic Matlab Question

From: jrenfree

Date: 16 Nov, 2009 23:59:29

Message: 2 of 13

On Nov 16, 3:46 pm, "James Carter" <jim...@msn.com> wrote:
> I have a 32 x 1300 2D array, lets call it S.  I know I can pick off a particular row with the following argument: A = S(1,:); which gives me a 1 x 1300 array from the first
> row of the 2D array.  I want every row assigned to a new indexed array, so I would start off with
>
> for n = 1:rows
> A(n) = S(n,:);
> end
>
> This doesn't work because of a dimensional mismatch.   I'm not really trying to define
> a dimension but simply declare a unique identifier.   How am I able to assign rows
> such that I can put them in A(1), A(2), etc., all with the dimension 1 x 1300.  
>
> Thanks!

So essentially you want A to be a 32x1 vector? You're trying to
access single elements of A, yet you're trying to put a 1x1300 length
vector into that single element.

You would either need to do A(n,:) = S(n, :) (which is pointless
because then A=S), or you need to make A either a structure or cell
array. That would A could be a 32x1 cell array, where each cell is
then a 1x1300 vector.

Subject: Basic Matlab Question

From: Matt Fetterman

Date: 17 Nov, 2009 00:01:04

Message: 3 of 13

"James Carter" <jimctr@msn.com> wrote in message <hdso7p$sh5$1@fred.mathworks.com>...
> I have a 32 x 1300 2D array, lets call it S. I know I can pick off a particular row with the following argument: A = S(1,:); which gives me a 1 x 1300 array from the first
> row of the 2D array. I want every row assigned to a new indexed array, so I would start off with
>
> for n = 1:rows
> A(n) = S(n,:);
> end
>
> This doesn't work because of a dimensional mismatch. I'm not really trying to define
> a dimension but simply declare a unique identifier. How am I able to assign rows
> such that I can put them in A(1), A(2), etc., all with the dimension 1 x 1300.
>
> Thanks!

How about a cell array:
 for n = 1:rows
 A{n} = S(n,:);
 end

Subject: Basic Matlab Question

From: Matt Fig

Date: 17 Nov, 2009 00:04:03

Message: 4 of 13

Use a cell array instead.
A{n} = S(n,:)

or easier:


A = [magic(3),magic(3)]
B = mat2cell(A,ones(1,size(A,1)),size(A,2))

Subject: Basic Matlab Question

From: James Carter

Date: 17 Nov, 2009 00:20:19

Message: 5 of 13

"Matt Fetterman" <mattinjersey@yahoo.com> wrote in message <hdsp40$mg5$1@fred.mathworks.com>...
> "James Carter" <jimctr@msn.com> wrote in message <hdso7p$sh5$1@fred.mathworks.com>...
> > I have a 32 x 1300 2D array, lets call it S. I know I can pick off a particular row with the following argument: A = S(1,:); which gives me a 1 x 1300 array from the first
> > row of the 2D array. I want every row assigned to a new indexed array, so I would start off with
> >
> > for n = 1:rows
> > A(n) = S(n,:);
> > end
> >
> > This doesn't work because of a dimensional mismatch. I'm not really trying to define
> > a dimension but simply declare a unique identifier. How am I able to assign rows
> > such that I can put them in A(1), A(2), etc., all with the dimension 1 x 1300.
> >
> > Thanks!

When I attempt your suggestion using the Cell Array A{n} I get the following error

??? Cell contents assignment to a non-cell array object.

>
> How about a cell array:
> for n = 1:rows
> A{n} = S(n,:);
> end

Subject: Basic Matlab Question

From: Doug

Date: 17 Nov, 2009 00:25:05

Message: 6 of 13

"James Carter" <jimctr@msn.com> wrote in message <hdso7p$sh5$1@fred.mathworks.com>...
> I have a 32 x 1300 2D array, lets call it S. I know I can pick off a particular row with the following argument: A = S(1,:); which gives me a 1 x 1300 array from the first
> row of the 2D array. I want every row assigned to a new indexed array, so I would start off with
>
> for n = 1:rows
> A(n) = S(n,:);
> end
>
> This doesn't work because of a dimensional mismatch. I'm not really trying to define
> a dimension but simply declare a unique identifier. How am I able to assign rows
> such that I can put them in A(1), A(2), etc., all with the dimension 1 x 1300.
>
> Thanks!

By definition A will be multidimensional since you have A(x) and each A(x) has a length of 1300.

If you need 1 x 1300 vectors you could use a unique name, i.e. A1=..., A2=...

for n = 1:rows
  q=['A' num2str(n) '= S(', num2str(n), ',:);'];
  eval(q);
end

or maybe use a cell array where each cell contains a 1x1300 array

c=cell(32,1);
for n = 1:rows
  c{n}=S(n,:);
end

Subject: Basic Matlab Question

From: James Carter

Date: 17 Nov, 2009 00:49:02

Message: 7 of 13

"Doug " <nospam@thx.com> wrote in message <hdsqh1$flv$1@fred.mathworks.com>...
> "James Carter" <jimctr@msn.com> wrote in message <hdso7p$sh5$1@fred.mathworks.com>...
> > I have a 32 x 1300 2D array, lets call it S. I know I can pick off a particular row with the following argument: A = S(1,:); which gives me a 1 x 1300 array from the first
> > row of the 2D array. I want every row assigned to a new indexed array, so I would start off with
> >
> > for n = 1:rows
> > A(n) = S(n,:);
> > end
> >
> > This doesn't work because of a dimensional mismatch. I'm not really trying to define
> > a dimension but simply declare a unique identifier. How am I able to assign rows
> > such that I can put them in A(1), A(2), etc., all with the dimension 1 x 1300.
> >
> > Thanks!

That did the trick. Thanks
>
> By definition A will be multidimensional since you have A(x) and each A(x) has a length of 1300.
>
> If you need 1 x 1300 vectors you could use a unique name, i.e. A1=..., A2=...
>
> for n = 1:rows
> q=['A' num2str(n) '= S(', num2str(n), ',:);'];
> eval(q);
> end
>
> or maybe use a cell array where each cell contains a 1x1300 array
>
> c=cell(32,1);
> for n = 1:rows
> c{n}=S(n,:);
> end

Subject: Basic Matlab Question

From: Matt Fig

Date: 17 Nov, 2009 00:50:04

Message: 8 of 13

DON'T use eval. Use mat2cell like I showed above.


http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Subject: Basic Matlab Question

From: Steven Lord

Date: 17 Nov, 2009 14:46:47

Message: 9 of 13


"Doug " <nospam@thx.com> wrote in message
news:hdsqh1$flv$1@fred.mathworks.com...
> "James Carter" <jimctr@msn.com> wrote in message
> <hdso7p$sh5$1@fred.mathworks.com>...
>> I have a 32 x 1300 2D array, lets call it S. I know I can pick off a
>> particular row with the following argument: A = S(1,:); which gives me a
>> 1 x 1300 array from the first
>> row of the 2D array. I want every row assigned to a new indexed array,
>> so I would start off with
>>
>> for n = 1:rows
>> A(n) = S(n,:);
>> end
>>
>> This doesn't work because of a dimensional mismatch. I'm not really
>> trying to define
>> a dimension but simply declare a unique identifier. How am I able to
>> assign rows
>> such that I can put them in A(1), A(2), etc., all with the dimension 1 x
>> 1300.
>>
>> Thanks!
>
> By definition A will be multidimensional since you have A(x) and each A(x)
> has a length of 1300.
>
> If you need 1 x 1300 vectors you could use a unique name, i.e. A1=...,
> A2=...

*snip*

NO. Do NOT use EVAL like this. See Q4.6 in the newsgroup FAQ for an
explanation why this is a Bad Idea.

> or maybe use a cell array where each cell contains a 1x1300 array
>
> c=cell(32,1);
> for n = 1:rows
> c{n}=S(n,:);
> end

To the OP: I would instead just use S(n, :) wherever in your code you would
have used A1, A2, ... or A(1), A(2), ... IMO it's no more difficult to read
than A1, A2, A(1), A(2), etc. and it avoids creating lots of smaller
variables in your workspace.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Basic Matlab Question

From: Doug

Date: 17 Nov, 2009 15:33:03

Message: 10 of 13

...
> NO. Do NOT use EVAL like this. See Q4.6 in the newsgroup FAQ for an
> explanation why this is a Bad Idea.
>
...
> To the OP: I would instead just use S(n, :) wherever in your code you would
> have used A1, A2, ... or A(1), A(2), ... IMO it's no more difficult to read
> than A1, A2, A(1), A(2), etc. and it avoids creating lots of smaller
> variables in your workspace.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

From the FAQ mentioned, "Now, if you still really want to create variables with dynamically generated names, you need to use EVAL. With EVAL, you use MATLAB commands to generate the string that will perform the operation you intend."

I agree this is not the best way to go about things and I wouldn't do it, but the FAQ itself spells out how to do what the OP wants to do using EVAL.

My advice to the OP is use what works for you to solve your problem. When you get more experience with Matlab, follow Steve's advice above and use S(n, :) instead of creating more variables.

Subject: Basic Matlab Question

From: Matt Fig

Date: 17 Nov, 2009 16:17:18

Message: 11 of 13

"Doug " <nospam@thx.com> wrote in message
> From the FAQ mentioned, "Now, if you still really want to create variables with dynamically generated names, you need to use EVAL. With EVAL, you use MATLAB commands to generate the string that will perform the operation you intend."
>
> I agree this is not the best way to go about things and I wouldn't do it, but the FAQ itself spells out how to do what the OP wants to do using EVAL.
>
> My advice to the OP is use what works for you to solve your problem. When you get more experience with Matlab, follow Steve's advice above and use S(n, :) instead of creating more variables.


I would say that this last bit is backwards. EVAL should be unknown to beginners and rarely used by advanced users. When a beginner starts using eval, he will come back to this NG with one (or more) of the following complaints:

1. My code is very slow, help me speed it up.
2. My code doesn't work and I cannot figure out why.
3. MATLAB closed by itself and I don't know why.

I have seen it again and again. EVAL has its place, but IMO, it is not in a beginner's hands.

Subject: Basic Matlab Question

From: Doug

Date: 18 Nov, 2009 15:10:23

Message: 12 of 13

"Matt Fig" <spamanon@yahoo.com> wrote in message
> >
> > My advice to the OP is use what works for you to solve your problem. When you get more experience with Matlab, follow Steve's advice above and use S(n, :) instead of creating more variables.
>
>
> I would say that this last bit is backwards. EVAL should be unknown to beginners and rarely used by advanced users. When a beginner starts using eval, he will come back to this NG ....

My advise was to "use what works for you to solve your problem". How could you possibly think that is backwards? If the OP gets results today he/she will be inclined to use Matlab again. If that person is scolded for not thinking like an experienced programmer they will avoid Matlab and this NG.

If that person runs into a problem later and comes back to this NG and gets more advice and learns more that way... why isn't that a good thing?

Subject: Basic Matlab Question

From: dpb

Date: 18 Nov, 2009 21:57:36

Message: 13 of 13

Doug wrote:
...
> My advise was to "use what works for you to solve your problem".
> How could you possibly think that is backwards? If the OP gets
> results today he/she will be inclined to use Matlab again. If that
> person is scolded for not thinking like an experienced programmer
> they will avoid Matlab and this NG.
>
> If that person runs into a problem later and comes back to this NG
> and gets more advice and learns more that way... why isn't that a
> good thing?

I'd say it's not a good thing to reinforce poor practice initially as
what one learns first is the hardest to unlearn.

--

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
eval Matt Fig 17 Nov, 2009 11:20:16
array manipulation James Carter 16 Nov, 2009 18:49:02
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com