I need help transforming data
古いコメントを表示
Hello,
I have multiple sets of nx3 matrices, where n is varying between the matrices and the matrices consist of coordinate points. I am trying to transform the data to get a matrix where the first column of my nx3 matrices (the x-coordinates) are the row indices, the 2nd column of the nx3 matrices (y-coordinates) are the column indices, and the 3rd column of the nx3 matrices (z-coordinates) are the values of the new matrix where the value matches with it's corresponding x and y indices (so the z-coordinate corresponds with its appropriate x and y coordinates). This is all to use the functions surf(Z) and surface(Z) in order to try and recreate an object in MATLAB. So far, I have the code
function Z = GetZmatrix(concatenatedmatrix)
Z=zeros(size(concatenatedmatrix,1)); %create matrix of zeroes
x = (1:size(concatenatedmatrix,1));
y = (1:size(concatenatedmatrix,2));
sorted = ceil(sortrows(concatenatedmatrix)) %sorts and gets rid of decimals
for i = sorted(:,1).'
for j = 1:size(concatenatedmatrix,1)
for l = sorted(:,2).'
for k = 1:size(concatenatedmatrix,2)
if i == j
if l == k
Z(k,j) = Z(k,j) + sorted(i,3);
end
end
end
end
end
end
However, this is not giving me the output I want for my test matrix. I created the matrix

in hopes of getting

However my code gave me

Can anyone help me understand why my code is giving the wrong output and maybe help me get what I'm trying to get? And I'm fairly new to MATLAB, so forgive my code if it is ugly or sloppy. Thanks! Michael
4 件のコメント
Michael J
2014 年 12 月 13 日
Star Strider
2014 年 12 月 14 日
What would you want to get in terms of a mathematical relation? It’s defined by Matrix ‘A’, so that’s about as good as it gets as a mathematical definition. You could certainly use fft2 to do the Fourier transform. What information do you want from it?
Michael J
2014 年 12 月 16 日
Star Strider
2014 年 12 月 16 日
Is the ‘object’ you’re referring to ‘A’ or ‘B’?
The only process I see maps the third column of ‘A’ by the column and row indices (respectively) given in its first two columns. So long as that remains unique, you simply have a discrete mapping.
採用された回答
その他の回答 (1 件)
Guillaume
2014 年 12 月 13 日
accumarray is what you want:
A = [1 2 4
5 6 4
3 2 1
1 1 1
2 3 4
4 1 5]
B = accumarray(A(:, [1 2]), A(:, 3))'
Note if you have identical x and y, the default behaviour of accumarray is to sum the z. You could use @min or @max or whatever you want if that were to happen and the sum doesn't suit you:
B = accumarray(A(:, [1 2]), A(:, 3), [], @min)'
2 件のコメント
Michael J
2014 年 12 月 16 日
What does
[r, c] = find(A(:, [1 2]) < 1 | mod(A(:, [1 2]), 1));
return?
If you get this error it's either because because some coordinates are not integer or are less than 1. Either way, you'll get the same error with Star Strider's answer.
You'll also get the same issue of creating a matrix big enough to hold all your points. The answer is also to create a sparse matrix, which accumarray allows you to do:
B = accumarray(A(:, [1 2]), A(:, 3), [], [], [], true);
Note that accumarray is probably faster than a handwritten loop.
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!