take the average of each column element of two rows (in a 2 dimensional array) and insert that average between the two rows

5 views (last 30 days)
I'm trying to take the average of 2 row elements in a 2 dimensional array and insert that average between the two.
For example,with the array:
2,3,4
4,5,6
should result in:
2,3,4
3,4,5
4,5,6
  3 Comments
Allan N
Allan N on 25 Jul 2014
Thank you for the fast response. I was planning to do this iteratively; so I'm wanting to put this routine in a IF loop, so that in the next go around, the average of rows 1 and 2 will be inserted between rows 1 and 2, and the average of rows 2 and 3 will be inserted between these 2 rows. And so on until the criteria is reached. Wich will do what I want? Wayne's or Andrei's?
Allan N
Allan N on 25 Jul 2014
So something like:
DO n1=1:repeat_until_necessary
DO n2=1:max_row_size
IF criteria_not_met
take_average_and_insert
end
end
end
I'd appreciate any help! Thank you!!

Sign in to comment.

Answers (3)

Andrei Bobrov
Andrei Bobrov on 24 Jul 2014
out = sort([mean(x);x])

dpb
dpb on 24 Jul 2014
Edited: Andrei Bobrov on 24 Jul 2014
The specific answer could be
x=[x(1,:);mean(x);x(2,:)];
As Wayne points out, you've not provided sufficient info to know precisely how to generalize to a more generic solution.

Image Analyst
Image Analyst on 25 Jul 2014
If you have the Image Processing Toolbox, you can simply use resize():
m=[2,3,4;...
4,5,6]
[rows, columns] = size(m); % Find out the size of m first.
% Insert mean rows.
out = imresize(m, [2*rows - 1, columns], 'bilinear')

Tags

Community Treasure Hunt

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

Start Hunting!