Expanding array by filling intermediate cells with mean of neighbour cells

3 views (last 30 days)
Hi,
I have 2 arrays and I want them to be nearly similar in length, however one array is of length 15 and the other is of length 30. So what I would like to do is double the length of the first array by putting in new cells in the array and put the mean of the neighbours in those cells.
For example: If we have the following array A.
A = [ 1 3 5 4 2]
Then I can create the following array B which is nearly double the size of A by just putting in the means of 2 neighbouring numbers in the cells between them.
B = [ 1 2 3 4 5 4.5 4 3 2]
Is there any function that does this automatically?

Answers (1)

Guillaume
Guillaume on 19 Oct 2014
You may be able to do something with interp1, but even without the help of any function, it's fairly trivial:
tmp = [A(1:end-1); ((A(1:end-1) + A(2:end))/2];
B = [tmp(:)' A(end)]
  1 Comment
Mathieu
Mathieu on 19 Oct 2014
Edited: Mathieu on 19 Oct 2014
Is it possible that the second line contains an error? I can't seem to get the tmp(:)' to run.
Nvm, forgot a space.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!