I want to construct a 3D matrix of size = 80 * 80 * 2
based on a set of data:
1 4532 1257.0
1 4556 1257.0
1 4622 257.0
1 4633 257.0
2 7723 31.0
2 8024 31.0
2 8099 31.0
2 9800 31.0
2 8524 34.0
2 8525 34.0
2 8700 734.0
2 8701 734.0
- The first column denotes the slice of matrix.
- The second column denotes the linear index of the matrix.
- The third column denotes the values of the elements.
What I'm doing now is: I first obtain two 80 * 80
2D matrices A
and B
and then concatenate them using cat(3, A, B)
:
Denote the above data be M
.
for i = 1 : size(M,1)
if (M(:,1)==1)
[r c]=ind2sub(M(:,2));
A = accumarray([r c], M(:,3));
elseif (M(:,1)==2)
[r c]=ind2sub(M(:,2));
B = accumarray([r c], M(:,3));
end
end
cat(3, A, B)
I am curious if there is any solutions that can build the 80*80*2
matrix merely by the linear index (the second column of my data) or any other simpler solution works for the purpose.
I appreciate for your help.
2
appears at the first column seems to be what you mean? – nam