Rearranging a vector into a matrix according to other two vectors

조회 수: 7 (최근 30일)
Mauricio
Mauricio 2024년 4월 18일
답변: Ayush Anand 2024년 4월 18일
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

답변 (2개)

KSSV
KSSV 2024년 4월 18일
편집: KSSV 2024년 4월 18일
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 2024년 4월 18일
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.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by