# Is there any way to get a matrix from data set which contains x position, y position and magnitude?

1 ビュー (過去 30 日間)
AAS 2020 年 9 月 4 日
コメント済み: Steven Lord 2020 年 9 月 5 日
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 件のコメント

Adam Danz 2020 年 9 月 4 日
Please provide examples of the x,y position data and the magnitudes.
For example, are they all vectors? Are x and y vectors while the magnitudes are a matrix? Are there any duplicate (x,y) values?
AAS 2020 年 9 月 4 日
X and Y are just position values and there is an additional value at each position. There can be duplicate X and Y values.
I basically have a set of points which has 3 components (x, y, magnitude). There can be many points that have x and y value but could have different magnitudes (maybe in this case, I can average them and input that into the matrix)
KSSV 2020 年 9 月 4 日
This can be done.....attach your data.

サインインしてコメントする。

### 採用された回答

Adam Danz 2020 年 9 月 4 日

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

#### 1 件のコメント

AAS 2020 年 9 月 5 日
Thank you very much!!

サインインしてコメントする。

### その他の回答 (1 件)

Steven Lord 2020 年 9 月 4 日
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 件のコメント

AAS 2020 年 9 月 4 日
If they arent positive integer values, is there any alternate way (Can we convert these X and Y values to some form of indices?) because what you have here is exactly what I need.
Thanks
Steven Lord 2020 年 9 月 5 日
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.

サインインしてコメントする。

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by