Is there any way to get a matrix from data set which contains x position, y position and magnitude?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I would like to generate a 2D matrix with rows being x position and y being y position and the matrix should be filled with magnitude at the points where it is detected. Sprry for the naive question. I do not have magnitudes at every points so some of the entries should be blank in the matrix.
4 commentaires
Réponse acceptée
Adam Danz
le 4 Sep 2020
Modifié(e) : Adam Danz
le 4 Sep 2020
Inputs are x, y, and m, which are your (x,y) coordinates and magnitudes, all the same length.
Outputs are magMat and T. The former is a matrix of mean magnitudes for each (x,y) coordinate where rows are the unique y values (yUnq) and columns are the unique x values (xUnq). The later is the same data displayed in a table.
% Create demo data
rng('default')
x = randi(8,1,20)-4;
y = randi(6,1,20)-3;
m = randi(10, 1,20); %magnitudes
% discretize magnitude values into unique (x,y) groups
[xyGroup, ~, xyID] = unique([x(:),y(:)], 'rows');
% Compute mean of each (x,y) group
magMean = accumarray(xyID, m, [size(xyGroup,1),1], @mean);
% Create magnitude matrix where columns are unique
% x values and rows are unique y values
xUnq = unique(x);
yUnq = unique(y);
magMat = nan(numel(yUnq), numel(xUnq));
[~, col] = ismember(xyGroup(:,1), xUnq);
[~, row] = ismember(xyGroup(:,2), yUnq);
ind = sub2ind(size(magMat), row, col);
magMat(ind) = magMean;
% summarize results in table
T = array2table(magMat, 'VariableNames', compose('x=%.1g',xUnq), 'RowNames', compose('y=%.1g',yUnq));
% T =
% 6×8 table
% x=-3 x=-2 x=-1 x=0 x=1 x=2 x=3 x=4
% ____ ____ ____ ___ ___ ___ ___ ______
% y=-2 NaN NaN NaN 2 NaN NaN 2 4.6667
% y=-1 NaN NaN NaN NaN NaN NaN NaN 6.3333
% y=0 NaN NaN NaN NaN 7 NaN NaN NaN
% y=1 NaN NaN NaN NaN NaN NaN 5 8
% y=2 5 4 5 10 NaN 2 NaN NaN
% y=3 NaN 8 NaN NaN NaN NaN 6 8
Plus de réponses (1)
Steven Lord
le 4 Sep 2020
Use either accumarray or sparse if the X and Y coordinates are positive integer values.
>> rng default
>> x = randi(4, 6, 1);
>> y = randi(4, 6, 1);
>> v = (1:6).';
>> coords = table(x, y, v)
>> A = accumarray([x, y], v, [4 4])
Since I called rng default you should have generated the same x and y as I did. This means you should see that two values in v, 3 and 6, are at the same coordinates (1, 4). Therefore A(1, 4) is 3+6 = 9.
The [4, 4] in the accumarray call ensures A would be a 4-by-4 matrix even if there were no entry in the 4th row or 4th column.
accumarray([1 1; 2 2; 3 3], [1; 1; 1]) % 3-by-3
accumarray([1 1; 2 2; 3 3], [1; 1; 1], [4 4]) % 4-by-4
2 commentaires
Steven Lord
le 5 Sep 2020
discretize your coordinates and use the bin numbers or see the "Unique Values and Their Indices" example on the documentation page for the unique function. The uniquetol function may also be of use.
Voir également
Catégories
En savoir plus sur Preprocessing Data dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!