1 view (last 30 days)

Adam Danz
on 4 Sep 2020

Edited: Adam Danz
on 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

Steven Lord
on 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

Steven Lord
on 5 Sep 2020

Opportunities for recent engineering grads.

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997213

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997213

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997261

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997261

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997276

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997276

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997336

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/589069-is-there-any-way-to-get-a-matrix-from-data-set-which-contains-x-position-y-position-and-magnitude#comment_997336

Sign in to comment.