matlab - Construct a 3D matrix with linear index -
i want construct 3d matrix of size = 80 * 80 * 2
based on 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 slice of matrix.
- the second column denotes linear index of matrix.
- the third column denotes values of elements.
what i'm doing is: first obtain 2 80 * 80
2d matrices a
, b
, concatenate them using cat(3, a, b)
:
denote above data m
.
for = 1 : size(m,1) if (m(:,1)==1) [r c]=ind2sub(m(:,2)); = 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 curious if there solutions can build 80*80*2
matrix merely linear index (the second column of data) or other simpler solution works purpose.
i appreciate help.
so, i'm assuming example data incorrect, , values in column 2 less n*n
, nxn
size of matrix (80x80 in case).
if that's case, following 2 lines should trick.
out = zeros(n,n,2); out((m(:,1)-1).*n^2+m(:,2)) = m(:,3)
if second column contains values 2*n*n, , linear indices, then:
out = zeros(n,n,2); out(m(:,2)) = m(:,3)
Comments
Post a Comment