f



Index exceeds matrix dimensions #21

function w = move_me(v,a)
    for x=1:numel(v)
       if v(x)==a
          v(x)=[];
          w(end)=a;
       else w(x)=v(x);
       end
   end
end
dear altruist
my third line (if v(x)==a) is getting error of "Index exceeds matrix dimensions". Would you please tell me why i am getting this?
I am quite new at matlab.
Thanks in advance
0
Rashedul
12/22/2016 5:54:03 AM
comp.soft-sys.matlab 211266 articles. 17 followers. lunamoonmoon (258) is leader. Post Follow

2 Replies
175 Views

Similar Articles

[PageSpeed] 32

On 12/21/2016 11:54 PM, Rashedul Chy wrote:
> function w = move_me(v,a)
>     for x=1:numel(v)
>        if v(x)==a
>           v(x)=[];
>           w(end)=a;
>        else w(x)=v(x);
>        end
>    end
> end
> dear altruist
> my third line (if v(x)==a) is getting error of
>"Index exceeds matrix dimensions". Would you please tell me why i am getting this?
> I am quite new at matlab.
> Thanks in advance
>

There are few things not right in the code, but it
will help if you show how you called the function.

ps. Matlab has a debugger build inside the editor,
you'd also be adviced to learn how to use it to help
you debug your code.

--Nasser
0
Nasser
12/22/2016 6:34:26 AM
"Rashedul Chy" wrote in message <o3fppr$hs8$1@newscl01ah.mathworks.com>...

> my third line (if v(x)==a) is getting error of "Index exceeds matrix dimensions". Would you please tell me why i am getting this?

Possibly because in the earlier iteration you delete an element (v(x) = []) of your vector so it get shorten. Then in the later iteration you still try to access to the end of the original vector.
0
Bruno
12/22/2016 7:34:03 AM
Reply: