3D Array Rastering

조회 수: 3 (최근 30일)
Dillon Kopecky
Dillon Kopecky 2020년 5월 28일
편집: Dillon Kopecky 2020년 5월 29일
I am attempting to raster scan DMD mirrors by inputing a 3D array. I would like the array (for the number of mirrors along one side, n = 2) to look like: [[1,0;0,0],[0,1;0,0],[0,0;1,0],[0,0;0,1]] (i.e. a row major order scan of the mirrors). I intend to scale this array and my initial attempt hasn't been succesful. How do I create this array?
N = 4;
Array3D = zeros(2,2,N);
for row=1:2
for col=1:2
for k=1:N
Array3D(row,col,k) = 1;
end
end
end
  댓글 수: 1
darova
darova 2020년 5월 29일
What about eye?

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2020년 5월 29일
편집: Matt J 2020년 5월 29일
I'm not sure from your description how this should generalize for different N, but perhaps this is what you want:
>> N=2; Array3D=reshape(eye(N^2),N,N,[])
Array3D(:,:,1) =
1 0
0 0
Array3D(:,:,2) =
0 0
1 0
Array3D(:,:,3) =
0 1
0 0
Array3D(:,:,4) =
0 0
0 1
  댓글 수: 2
Matt J
Matt J 2020년 5월 29일
편집: Matt J 2020년 5월 29일
Incidentally, for large N, the array you are trying to building (assuming I've understand the goal correctly) will be highly RAM-consuming. You can reduce memory consumption by using my ndSparse class (Download). E.g.,
>> N=30; Array3D=reshape(eye(N^2),[N,N,N^2]); SparseArray3D=ndSparse(speye(N^2),[N,N,N^2]);
>> whos *Array3D
Name Size Kilobytes Class Attributes
Array3D 30x30x900 6329 double
SparseArray3D 30x30x900 22 ndSparse
>> isequal(Array3D,SparseArray3D)
ans =
logical
1
Dillon Kopecky
Dillon Kopecky 2020년 5월 29일
편집: Dillon Kopecky 2020년 5월 29일
Excellent! Thank you! I knew I must have been overcomplicating the solution. Thanks for the heads up regarding optimization and providing your code.
I needed row major operation which may not have initially been clear. The following code yields the correct ordering in case someone is interested.
N=3;
A = eye(N^2);
Array3D=reshape(A',N,N,[]);
B = permute(Array3D,[2 1 3]);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by