Kostas_Fr
Kostas_Fr

Reputation: 57

How can i divide each row of a vector with its next row value in matlab

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

Answers (1)

Aabaz
Aabaz

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

Related Questions