# Rearranging a vector into a matrix according to other two vectors

5 views (last 30 days)
Mauricio on 18 Apr 2024
Answered: Ayush Anand on 18 Apr 2024
Hi everybody
I have three matrices (latitude, depth, concentration; 6112 x 1) for which I am dealing with rearrangement.
I used unique() to use the total range of latitude and depth, now as (1,78) and (1,2241). I would like to create a matrix with my concentration to combine the respective values of latitude and depth, than, the concentration should have (78,2241).
Mauricio
Stephen23 on 18 Apr 2024

KSSV on 18 Apr 2024
Edited: KSSV on 18 Apr 2024
x = unique(lat) ;
y = unique(depth) ;
[X,Y] = meshgrid(x,y) ;
F = scatteredInterpolant(lat,depth,concentration,'linear','none') ;
Z = F(X,Y) ;
h = pcolor(X,Y,Z)
h.EdgeColor = 'none' ;
Also have a look on griddata.
If your data corresponds to structured grid, you can reshape concentration and other data. You maay refer this link: https://in.mathworks.com/matlabcentral/answers/1997568-how-to-integrate-a-vector-over-a-surface-of-x-y-coordinate-vectors

Ayush Anand on 18 Apr 2024
You can do this by defining a new concentration_matrix of shape (78,2241) with NaN values and iterating through the indices of the latitude(rows) and depth(columns) vectors as obtained after sorting them using the unique function. For each row and column index value, fill in the corresponding concentration value in the new concentration_matrix.
% Find the unique values and their indices
[lat_unique, ~, lat_idx] = unique(latitude, 'sorted');
[depth_unique, ~, depth_idx] = unique(depth, 'sorted');
% Initialize the new concentration matrix
concentration_matrix = NaN(length(lat_unique), length(depth_unique));
% Loop through the indices
for i = 1:length(latitude)
row = lat_idx(i);
column = depth_idx(i);
% Assign the concentration value to the correct position
concentration_matrix(row, column) = concentration(i);
end
% concentration_matrix now has the shape (78, 2241) with the concentrations arranged
% according to the corresponding latitude and depth.