Interpolating many matrices without for loop

Hi everybody, well I want to interpolate a 3D matrix composed of 700x2x60 values. I want to interpolate with respect to an array of 700 values. But i want an interpolation only 1 dimension, for example
if i had:
3 1
6 2
9 3
60 times, with different values, i want to interpolate with respect to 1.5, 2.5 and 3 I would get 4.5 7.5 and 10.5, 60 times and with the values relative to each one of the 60 matrices.
So in essence its like interp1 but 60 times. And I can't manage to do it without a for loop which is time consuming. and i need to do it over 15000 times.
thank you for your answers

5 Comments

Without proper {}Code format, it's actually very hard to understand your question.
the format has been changed a bit. The 3 1 6 2 9 3 is a 3x2 matrix where 3 1 6 is the first column and 2 9 3 the second one.
I have also tried to repmat the array 60 times and use interp2, however it doesn't seem to work right, or I am doing wrong. But I don't think that interp2 is what i need.
thanks again
how can I put the question in proper format?
You can edit your question and apply {}Code format to make it look right. I did only the {}Code formatting part but didn't change any of your text.
Look at all the buttons above your question text box when you post or edit your question.

Sign in to comment.

 Accepted Answer

You can use reshape().
x=1:3;
y=3:3:9;
X=magic(3);
[M,N]=size(X);
Y=interp1(x,y,X(:),'linear','extrap');
Y=reshape(Y,M,N);

5 Comments

Ty for the format info, Fangjun.
I'm not sure if my question is clear. Right now I have this:
initial = matrix(700x2x60)
I separate into
a = matrix(700x60);
b = matrix(700x60);
and build an array with values similar to b but not exactly
c = array(700x1);
c = repmat(c,700,1,60)
final = zeros(700x2x60)
final(:,2,:) = c;
for i = 1:60
final(:,1,i) = interp1(a(:,i),b(:,i),c(i));
end
and this works, however I don't like the for loop.
Maybe your answer is right and if so I don't seem to understand
ty
I understand your algorithm now. I don't see an obvious way to avoid the for-loop. arrayfun() might work but in my opinion, that just hides the for-loop.
Why so obsessed with no loops? Numerous examples have shown that no loop doesn't necessarily mean faster.
I am building a very large simulation program which needs to do many iterations > 15000, (i can't avoid that for loop), with a lot of processing in each iteration. I had bad software(many for loops) and each iteration was taking many seconds(even a minute) which means a veeeery long time. I have managed to reduce time of each iteration to a few seconds, aprox, and i would like to bring it down even more, so I am trying to get rid of all for loops.
But it is true that I have taken some away which have barely reduced my time, and probably it is not worth it eliminating all, but any I can, I try.
thanks once again for everything......... see u in another for loop
Matlab's INTERP1 is lame and there is no reason to limit the algorithm to vectors. But a linear interpolation is not complicated - roughly:
Si = Ti - floor(Ti);
Ti = floor(Ti);
Yi = Y(Ti, :) .* (1 - Si) + Y(Ti + 1, :) .* Si;
I do not understand the value of c. It is a [700x1x60] array any you use c(i) for the interpolation.
Really c could be a 700x1 array, but since it is going to be the second column of final I replicate it 60 times so I can insert it in final.
Then I use only one(really it could be c(1) instead of c(i)) for the interpolation

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!