How to increase the size of a vector while maintaining its original shape? MATLAB

Asked

Viewed 646 times

0

I have two vectors, one has vector X of 729 elements and the other vector Y 429 elements, but I needed to assemble a graph (vector X, vector Y), however they have different sizes and this is not possible

So I needed to know if there is any way to "lengthen" the size of vector Y from 429 to 729 elements, but I wanted it to maintain its original shape and be able to do this transformation

  • I just realized that you should explain what you mean by original form! You say corresponding values?

1 answer

1

The answer to that question is Interpolation!.

However, you should not lengthen the value of Y, but decrease the value of X! The reason for this is that reducing the number of values in general has more precision than the other way around. Obviously, interpolation exists because it is not always possible. That said, follow two examples.

Here I use the function interp1, but there are others in the link I put before. And a comment very important: There are many ways to make this interpolation, besides other functions you can use or not the pair (X,Y). Here I made it easier for me given your statement!

%dados
a=1:10
a=
 1 2 3 4 5 6 7 8 9 10
%Aumentar
b=interp1(a,linspace(min(a),max(a),19))
b=
 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5 10
%diminuir
b=interp1(a,linspace(min(a),max(a),4))
b= 1 4 7 10

See that the numbers have been selected to be beautiful for this answer, but the function works with any value.

Another important comment is to know how the numbers are distributed. This method works well when the values are distributed homogeneously (1,2,3,4), but can give problems in another case (1,4,5,6). Interpolation is a complicated thing, when you have more time maybe increase the discussion with quick examples for two cases and more methods.

  • 1

    dei +1, but be careful with this type of example it will only work right for a sequential vector as shown in var a

Browser other questions tagged

You are not signed in. Login or sign up in order to post.