Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: matlab code doesnt work

Subject: matlab code doesnt work

From: kaancho12

Date: 16 Dec, 2007 04:50:41

Message: 1 of 5

Hi,
I am trying to implement this code (which was on a lecture slide that I found on the internet) but I could not figure out the error code. Any help is appreciated:
price = [74.9 108.34 82.52 47.2 10 8;
78.85 108.9 82.7 47.34 11 6;
79.86 118 83.3 49.44 12 9;
88.53 120.05 81.75 48.23 11 9;
88.84 134.31 81.13 49.25 12 8
];


[n,col] = size(price);
for i=2:n
ret(i,:) = (price(i,:)-price(i-1,:))./price(i-1,:);
end

The error I get is:
??? Error using ==> run
Subscripted assignment dimension mismatch.

Any idea how to modify my subscript to make this work?

Subject: Re: matlab code doesnt work

From: John O'Flaherty

Date: 16 Dec, 2007 05:32:19

Message: 2 of 5

On Sat, 15 Dec 2007 23:50:41 EST, kaancho12 <kaancho12@hotmail.com>
wrote:

>Hi,
>I am trying to implement this code (which was on a lecture slide that I found on the internet) but I could not figure out the error code. Any help is appreciated:
>price = [74.9 108.34 82.52 47.2 10 8;
>78.85 108.9 82.7 47.34 11 6;
>79.86 118 83.3 49.44 12 9;
>88.53 120.05 81.75 48.23 11 9;
>88.84 134.31 81.13 49.25 12 8
>];
>
>
>[n,col] = size(price);
>for i=2:n
>ret(i,:) = (price(i,:)-price(i-1,:))./price(i-1,:);
>end
>
>The error I get is:
>??? Error using ==> run
>Subscripted assignment dimension mismatch.
>
>Any idea how to modify my subscript to make this work?

It seems to work here- this is the result:
ret = 0 0 0 0 0 0
    0.0527 0.0052 0.0022 0.0030 0.1000 -0.2500
    0.0128 0.0836 0.0073 0.0444 0.0909 0.5000
    0.1086 0.0174 -0.0186 -0.0245 -0.0833 0
    0.0035 0.1188 -0.0076 0.0211 0.0909 -0.1111

Maybe there's a difference between what you pasted in the message and
what you're running.
--
John

Subject: Re: matlab code doesnt work

From: Roger Stafford

Date: 16 Dec, 2007 06:38:49

Message: 3 of 5

kaancho12 <kaancho12@hotmail.com> wrote in message
<11132067.1197780702950.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hi,
> I am trying to implement this code (which was on a lecture slide that I
found on the internet) but I could not figure out the error code. Any help is
appreciated:
> price = [74.9 108.34 82.52 47.2 10 8;
> 78.85 108.9 82.7 47.34 11 6;
> 79.86 118 83.3 49.44 12 9;
> 88.53 120.05 81.75 48.23 11 9;
> 88.84 134.31 81.13 49.25 12 8
> ];
>
>
> [n,col] = size(price);
> for i=2:n
> ret(i,:) = (price(i,:)-price(i-1,:))./price(i-1,:);
> end
>
> The error I get is:
> ??? Error using ==> run
> Subscripted assignment dimension mismatch.
>
> Any idea how to modify my subscript to make this work?
-------
  In all likelihood, you are running this, having already created a 'ret' array of
a different size from the above 5 by 6, and matlab complains about the
mismatch in size that it sees.

  In any case you should always preallocate to the proper size such arrays that
are to be filled up step-by-step in a for-loop so that their memory can be set
aside ahead of time rather than being expanded in a series of steps. That will
improve execution time.

Roger Stafford




Subject: Re: matlab code doesnt work

From: kaancho

Date: 18 Dec, 2007 21:40:02

Message: 4 of 5

thanks for all the replies. The code indeed works. For some reason when i had to close matlab and restart it to take the data properly...maybe like Roger said I need to initialize the array.

Subject: Re: matlab code doesnt work

From: Jos

Date: 18 Dec, 2007 22:00:09

Message: 5 of 5

kaancho <kaancho12@hotmail.com> wrote in message
<10642267.1198014032538.JavaMail.jakarta@nitrogen.mathforum.
org>...
> thanks for all the replies. The code indeed works. For
some reason when i had to close matlab and restart it to
take the data properly...maybe like Roger said I need to
initialize the array.


You should not need to close matlab. Issueing a "clear ret"
or even better (!) pre-allocate ret to it's final size
(i.e., initializing) should do.

The error you get is similar to

a = ones(3,2) ;
a(1,:) = 1:10 ;

Jos

Tags for this Thread

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
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 Disclaimer prior to use.
Related Topics