Reputation: 57
Suppose i have the vector a=[1;2;2]
i want to create the vector b=[1/2;1;2]
deviding each value of the ith
row of a with the ith+1
value, the last value can't be divided with anything so i let like it is. I made a simple code, but i get the following error message: "Index exceeds matrix dimensions", so i need your help. Example code
a=rand(3,1);
for i=1:length(a)
b(i)=a(i)/a(i+1)
end
Thanks a lot for your help
Upvotes: 2
Views: 665
Reputation: 3116
You can try :
b=a./[a(2:end);1];
Not pretty but it works.
The error message "Index exceeds matrix dimensions" comes from your try to reference a(i+1) when i=length(a) since this element does not exist.
Upvotes: 4