MATLAB: How to merge similar matrixes and list them on columns.

columnslatitudelongitudematrix

Hi Matlab users,
OK, so I have a 238×132 matrix representing current movements in one day(let's call it U), and two 238×132 matrixes representing one Latitude and one Longitude (they are like this because they are gridded). What I need to do is to put all three of them on columns like this: Latitude Longitude U where Latitude and Longitude have the same positions inside their matrixes as U.
I can do it like this: U11=[Latitude(1,1) Longitude(1,1) U1(1,1)]; but it takes a lot of time. Is there a easier way?
I am really ashamed for not knowing this.

Best Answer

Umn = [Latitude(:) Longitude(:) U(:)];
other variant
M = cat(3,Latitude,Longitude,U);
C = cellfun(@(x)x(:)',num2cell(M,3),'un',0);
out = cell2mat(C);
more variant
M = cat(3,Latitude,Longitude,U);
out = reshape(permute(M,[3 2 1]),[],size(U,1))';
or
M = [Latitude(:),Longitude(:),U(:)];
out = reshape(permute(reshape(M',3,size(U,1),[]),[2 1 3]),size(U,1),[]);
Related Question