Four Indexing of a two index matrix
1 次查看(过去 30 天)
显示 更早的评论
Hallo, my problem is that i want to address each element of a matrix (2index) by a 4index number and vice versa.
for example, a typicall matlab matrix "A" has the 2 index addressing
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
I want to make matrix "A" to have four index addressing ie
1111 1112 1121 1122
1211 1212 1221 1222
2111 2112 2121 2122
2211 2212 2221 2222
... So instead of writting A(1,1) i would instead writte A(1,1,1,1) (The reshape command doesnt give me right results for the 36x36 matrices i use, i believe that it reshapes the matrix not the way of the example)
Thank you all in advance.
0 个评论
采纳的回答
Walter Roberson
2011-11-24
Doing that and being consistent with the rest of MATLAB would require that you write a new MATLAB Object class with its own subsref() and subsasgn() methods, and possibly reshape() as well. You are asking that data be stored in a different order than MATLAB normally stores it. All kinds of routines (such as sum() and mean()) expect data to be stored in the MATLAB order.
However, it is possible that for your intended use, you just need to know how to re-order a vector with (36 * 36) elements so that it fills memory in to a 4D array with your desired order. If so, that can be done with transpose() and flipud() and fliplr() and permute() and reshape() in combination. You might not need all of those in practice for this particular order. (I haven't yet worked out a good reordering sequence for this.)
更多回答(1 个)
Jan
2011-11-24
A = [1111 1112 1121 1122;
1211 1212 1221 1222;
2111 2112 2121 2122;
2211 2212 2221 2222];
B = reshape(A, 2,2,2,2);
C = permute(B, [2,1,4,3]);
>> ans(:,:,1,1) =
1111 1211
2111 2211
ans(:,:,2,1) =
1121 1221
2121 2221
ans(:,:,1,2) =
1112 1212
2112 2212
ans(:,:,2,2) =
1122 1222
2122 2222
Now the i.th index is equivalent to the i.th digit in the number. This works for the 4x4 matrix only and I cannot predict, what you need for your 36x36 matrix. But the method will be very similar.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!