InquilineKea
InquilineKea

Reputation: 891

How do I resize a Matlab matrix with a 3rd dimension?

So I'd like to resize a matrix that is of size 72x144x156 into a 180x360x156 grid. I can try to do it with this command: resizem(precip,2.5). The first two dimensions are latitude and longitude, while the last dimension is time. I don't want time to be resized.

This works if the matrix is of size 72x144. But it doesn't work for size 72x144x156. Is there a way to resize the first two dimensions without resizing the third?

Also, what is the fastest way to do this (preferably without a for loop). If a for loop is necessary, then that's fine.

Upvotes: 1

Views: 754

Answers (3)

chappjc
chappjc

Reputation: 30579

I hinted in my comment, but could use interp3 like this:

outSize = [180 360 156];
[nrows,ncols,ntimes] = size(data);
scales = [nrows ncols ntimes] ./ outSize;

xq = (1:outSize(2))*scales(2) + 0.5 * (1 - scales(2));
yq = (1:outSize(1))*scales(1) + 0.5 * (1 - scales(1));
zq = (1:outSize(3))*scales(3) + 0.5 * (1 - scales(3));
[Xq,Yq,Zq] = meshgrid(xq,yq,zq);
dataLarge = interp3(data,Xq,Yq,Zq);

But the problem is simplified if you know you don't want to interpolate between time points, so you can loop as in Daniel R's answer. Although, this answer will not increase the number of time points.

Upvotes: 2

Ben Lin
Ben Lin

Reputation: 835

    % s = zeros(72, 144, 156);
    % whos s;
    % news = resize2D(s, 2.5);
    % whos news;

    function [result] = resize2D(input, multiply)
        [d1, d2, d3] = size(input);
        result = zeros(d1*multiply, d2*multiply, d3);
    end

Upvotes: 1

Daniel
Daniel

Reputation: 36710

D= %existing matrix
scale=2.5;
E=zeros(size(D,1)*2.5,size(D,2)*2.5,size(D,3))
for depth=1:size(D,3)
  E(:,:,depth)=resizem(D(:,:,depth),scale)
end

This should provide the expected output.

Upvotes: 1

Related Questions