Also, would it be possible to get a mathematical equation of this object? I'm not sure if using the Fourier series or doing an FFT would work or not.
I need help transforming data
조회 수: 1 (최근 30일)
이전 댓글 표시
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146451/image.png)
in hopes of getting
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146452/image.png)
However my code gave me
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146453/image.png)
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
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.
채택된 답변
Star Strider
2014년 12월 13일
편집: Star Strider
2014년 12월 13일
Alternatively, a very simple implementation with one loop is:
A = [1 2 4; 5 6 4; 3 2 1; 1 1 1; 2 3 4; 4 1 5];
B = zeros(6);
for k1 = 1:size(A,1)
B(A(k1,2),A(k1,1)) = A(k1,3);
end
댓글 수: 6
Guillaume
2014년 12월 16일
I am chilled. I'm just pointing a slightly more efficient way (in my opinion) to write the loop (saves on a indexing operation).
Note that I did it as a comment to your answer, so Michael would still accept your answer if he wanted to go with the loop.
Star Strider
2014년 12월 16일
It seems indexing operations aren’t what Michael J wants anyway.
He’s looking for a mathematical relation that defines the indexing operation.
I don’t see it, and a linear regression (of columns 1:2 against column 3) is a poor fit. (We don’t have the data that created the ‘A’ matrix.) Until we learn more, I doubt any answer (thus far) is what he’s looking for.
추가 답변 (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
Guillaume
2014년 12월 16일
편집: Guillaume
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!