Clear Filters
Clear Filters

Rearranging a vector into a matrix according to other two vectors

2 views (last 30 days)
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).
Thank you in advance for your help,
Mauricio

Answers (2)

KSSV
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
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!