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:
dimensional error

Subject: dimensional error

From: Sepehr Sadighpour

Date: 26 May, 2010 00:40:09

Message: 1 of 4

Hi. I am trying to create a three-dimensional array using the following nested for-loop. A note on the wfbm function: it outputs a "random" row vector of length lg. What I want is a 3D matrix called "fbm100" that has a 1000 rows of these length lg vectors, and a "depth" or third dimension of "Hsize" as defined below.

I've commented on the line where I'm receiving the error in running this script:

lg=100; %length of pseudo-random walk%
Hinc = 0.05; Hstart = 0.05; Hend = 0.95; %degree of randomness - variable%
H = Hstart:Hinc:Hend;
Hsize=length(H);
fbm100=zeros(1000,lg,Hsize); %preallocating for speed%

for H = Hstart:Hinc:Hend;
    for n=1:1000;
        randn('state',1)
        fbm100(:,n,H*20) = wfbm(H,lg);
    end
end

The error is:

??? Subscripted assignment dimension mismatch.

Error in ==> fbm at 14
        fbm100(:,n,H*20) = wfbm(H,lg);

I figured because H is in 0.05 increments starting at 0.05 and going to 0.95, multiplying it by 20 would yield integers that could work as indices for the third dimension of fbm100. Any light you can shed on this would be really appreciated.

Thanks,
Sepehr

Subject: dimensional error

From: Roger Stafford

Date: 26 May, 2010 00:56:04

Message: 2 of 4

"Sepehr Sadighpour" <sepehr125@gmail.com> wrote in message <hthql9$da0$1@fred.mathworks.com>...
>
> I figured because H is in 0.05 increments starting at 0.05 and going to 0.95, multiplying it by 20 would yield integers that could work as indices for the third dimension of fbm100. Any light you can shed on this would be really appreciated.
> .....

  That's where you made your mistake! With floating point numbers on any binary machine, they cannot represent .05 exactly, so that upon multiplication by 20, they don't always yield an exact integer. You need to alter your program to use integer indices directly and work out the fractional quantities from those indices.

Roger Stafford

Subject: dimensional error

From: TideMan

Date: 26 May, 2010 02:07:03

Message: 3 of 4

On May 26, 12:56 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "Sepehr Sadighpour" <sepehr...@gmail.com> wrote in message <hthql9$da...@fred.mathworks.com>...
>
> > I figured because H is in 0.05 increments starting at 0.05 and going to 0.95, multiplying it by 20 would yield integers that could work as indices for the third dimension of fbm100. Any light you can shed on this would be really appreciated.
> > .....
>
>   That's where you made your mistake!  With floating point numbers on any binary machine, they cannot represent .05 exactly, so that upon multiplication by 20, they don't always yield an exact integer.  You need to alter your program to use integer indices directly and work out the fractional quantities from those indices.
>
> Roger Stafford

As well as that, I think you've got the first two indexes arse about
face.
Shouldn't it be:
fbm100(n,:,H*20) =
not
fbm100(:,n,H*20) =

And the best way to fix the other problem is to replace
for H = Hstart:Hinc:Hend;
with
for ih=1:Hsize
then
fbm100(:,n,ih)=wfbm(H(ih),lg);

Subject: dimensional error

From: us

Date: 26 May, 2010 07:33:05

Message: 4 of 4

"Sepehr Sadighpour"
> for n=1:1000;
> randn('state',1)
> fbm100(:,n,H*20) = wfbm(H,lg);
> end

others have shown you solutions to your problem...
just another thought:
- do NOT call
     randn('state',1);
  in a loop(!)...
- there have been a number of posts in this NG dealing with this;
  look for rand/state/etc...

us

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