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:
trouble filling a multidimensional array with a for loop

Subject: trouble filling a multidimensional array with a for loop

From: Kirk

Date: 29 Jun, 2011 15:24:09

Message: 1 of 5

I am having trouble with the syntax to fill a multidimensional array with a for loop. I am trying to loop through an m x n x p array filling each p dimension with the output of a function such as repmat, or custom built year incriminator.

For example createYr2 and repmat work, but only on the first p array, not on any subsequent p's.

n = 10;

% Preallocate climate array
climate=zeros(14400, 10, n);

% Loop through the number of interations
for i = 1:n;
 
    climate(1:14400,1) = createYr2(meanClim.year);
    climate(1:14400,2) = repmat(meanClim.doy,[1200,1,:]);
    
end;


Results:

val(:,:,1) =

  Columns 1 through 5

        1001 16 0 0 0
        1001 46 0 0 0
        1001 75 0 0 0
        1001 106 0 0 0
        1001 136 0 0 0
        1001 167 0 0 0
        1001 197 0 0 0
        1001 228 0 0 0
        1001 259 0 0 0
        1001 289 0 0 0
        1001 320 0 0 0
        1001 350 0 0 0
        ...

val(:,:,2) =

     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     ...


If I try and ad the third argument like this:
climate(1:14400,1,:) = createYr2(meanClim.year);

I get the error:
??? Assignment has fewer non-singleton rhs dimensions than
non-singleton
subscripts


Any suggestions would be appreciated.


Error in ==> mcclim at 116
    climate(1:14400,1,:) = createYr2(meanClim.year);

Subject: trouble filling a multidimensional array with a for loop

From: Steven_Lord

Date: 29 Jun, 2011 15:36:16

Message: 2 of 5



"Kirk" <kwythers.nospam@umn.edu> wrote in message
news:iufg2p$l94$1@newscl01ah.mathworks.com...
> I am having trouble with the syntax to fill a multidimensional array with
> a for loop. I am trying to loop through an m x n x p array filling each p
> dimension with the output of a function such as repmat, or custom built
> year incriminator.
> For example createYr2 and repmat work, but only on the first p array, not
> on any subsequent p's.
> n = 10;
>
> % Preallocate climate array
> climate=zeros(14400, 10, n);
>
> % Loop through the number of interations
> for i = 1:n;
>
> climate(1:14400,1) = createYr2(meanClim.year);

Don't you mean

climate(:, :, i) = createYr2(meanClim.year);

instead? This fills in all rows and all columns of page i of the climate
array with the matrix created by createYr2.

> climate(1:14400,2) = repmat(meanClim.doy,[1200,1,:]);

This won't work and should have given you a syntax error immediately; having
: alone like that as an element of a vector doesn't make sense. Using : in
an indexing expression does, as in that context MATLAB can use the size of
the variables involved in the indexing expression to figure out what :
represents. In my suggested replacement, MATLAB knows climate is a variable
of size [14400 10 n] and so the first colon in the indexing expression
represents 1:14400 and similarly for the second colon.

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

Subject: trouble filling a multidimensional array with a for loop

From: Kirk

Date: 29 Jun, 2011 15:50:30

Message: 3 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message
> Don't you mean
>
> climate(:, :, i) = createYr2(meanClim.year);
>
> instead? This fills in all rows and all columns of page i of the climate
> array with the matrix created by createYr2.

Yup... That did it. Thank you Steve.

Subject: trouble filling a multidimensional array with a for loop

From: Kirk

Date: 29 Jun, 2011 16:06:09

Message: 4 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message
>
> Don't you mean
>
> climate(:, :, i) = createYr2(meanClim.year);
>
> instead? This fills in all rows and all columns of page i of the climate
> array with the matrix created by createYr2.
>
> > climate(1:14400,2) = repmat(meanClim.doy,[1200,1,:]);
>
> This won't work and should have given you a syntax error immediately; having
> : alone like that as an element of a vector doesn't make sense. Using : in
> an indexing expression does, as in that context MATLAB can use the size of
> the variables involved in the indexing expression to figure out what :
> represents. In my suggested replacement, MATLAB knows climate is a variable
> of size [14400 10 n] and so the first colon in the indexing expression
> represents 1:14400 and similarly for the second colon.
>
Here's another quick question...

In filling the 3rd column I added the line:

    climate(:,1,i) = createYr2(meanClim.year);
    climate(:,2,i) = repmat(meanClim.doy,[1200,1]);
    climate(1:12000,3,i) = repmat(meanClim.tmax,[1000,1]);

but MATLAB did this:

val(:,:,1) =

   1.0e+03 *

  Columns 1 through 7

    1.0010 0.0160 -0.0087 0 0 0 0
    1.0010 0.0460 -0.0060 0 0 0 0
    1.0010 0.0750 0.0016 0 0 0 0
    1.0010 0.1060 0.0117 0 0 0 0
    1.0010 0.1360 0.0194 0 0 0 0
    1.0010 0.1670 0.0241 0 0 0 0
    1.0010 0.1970 0.0272 0 0 0 0
    1.0010 0.2280 0.0262 0 0 0 0
    1.0010 0.2590 0.0206 0 0 0 0
    1.0010 0.2890 0.0135 0 0 0 0
    1.0010 0.3200 0.0024 0 0 0 0
    1.0010 0.3500 -0.0057 0 0 0 0

The values in the struct meanClim.tmax (12x1 double) are:

-8.70280260000000
-5.98621180000000
1.57039086000000
11.7400682000000
19.4205277000000
24.0980430000000
27.2233466000000
26.2097043000000
20.5885815000000
13.4547500000000
2.39835922000000
-5.73869720000000

why did MATLAB convert the display to:


val(:,:,1) =

   1.0e+03 *

Can I force MATLAB to stick to regular numerical display?

Subject: trouble filling a multidimensional array with a for loop

From: Steven_Lord

Date: 29 Jun, 2011 17:11:07

Message: 5 of 5



"Kirk" <kwythers.nospam@umn.edu> wrote in message
news:iufihh$sg8$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message

*snip*

> Here's another quick question...
>
> In filling the 3rd column I added the line:
>
> climate(:,1,i) = createYr2(meanClim.year);
> climate(:,2,i) = repmat(meanClim.doy,[1200,1]);
> climate(1:12000,3,i) = repmat(meanClim.tmax,[1000,1]);
>
> but MATLAB did this:
>
> val(:,:,1) =
>
> 1.0e+03 *
>
> Columns 1 through 7
>
> 1.0010 0.0160 -0.0087 0 0 0 0

*snip*

> why did MATLAB convert the display to:

Because you have elements with quite varied magnitudes, and that's the way
the default display format handles that situation.

> Can I force MATLAB to stick to regular numerical display?

Use the FORMAT function to control how MATLAB displays the array. I tend to
use "format long g" but that's just my preference.

Note that FORMAT has absolutely no impact on how the data is actually
_stored_, how computations are performed, or how the data is processed by
file I/O functions; it ONLY impacts how the data is _displayed_ to the
screen.

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

Tags for this Thread

No tags are associated with 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