How can I permute a 3D magic matrix such that the largest element will be on page 3.

A1=[25 16 80 104 90;115 98 4 1 97;42 111 85 2 75;66 72 27 102 48;67 18 119 106 5];
A2=[91 77 71 6 70;52 64 117 69 13;30 118 21 123 23;26 39 92 44 114;116 17 14 73 95];
A3=[47 61 45 76 86;107 43 38 33 94;89 68 63 58 37;32 93 88 83 19;40 50 81 65 79];
A4=[31 53 112 109 10;12 82 34 87 100;103 3 105 8 96;113 57 9 62 74;56 120 55 49 35];
A5=[121 108 7 20 59;29 28 122 125 11;51 15 41 124 84;78 54 99 24 60;36 110 46 22 101];
A=cat(3,A1,A2,A3,A4,A5);
In this example the largest is on page 5

4 Kommentare

Peter - what is page 3, or page 5? Are you referring to a textbook?
Peter - so do you want all combinations of the 5 matrices such that the third will always have A5? So the following would be considered permutations of A
A1 A2 A5 A3 A4
A1 A2 A5 A4 A3
A1 A3 A5 A2 A4
A1 A3 A5 A4 A2
A1 A4 A5 A2 A3
A1 A4 A5 A3 A2
etc.
Rememeber is a 3D array/multidimensional matrix (A=cat(3,A1,A2,A3,A4,A5)). I want its permutation such that the largest element (125) will be on page three, not necessary the whole A5, and still keeping the properties of magic matrix.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

By "permutation" I assume you mean a permutation among the 3D pages.
[~,ind] = max(A(:));
[~,~,i3] = ind2sub(size(A),ind);
p = 1:size(A,3); p([3,i3]) = p([i3,3]);
A = A(:,:,p);
Note: There are many other permutations of the pages (23 to be exact) that will move the largest element to page 3. This one is a simple swap.

6 Kommentare

Thank you for your quick response. I appreciate you. I forgot to include that the central element (63) must not move. It has to be fixed.
The result of your code gave sum along the diagonal as 271 instead of 315. You can achieve this by keeping the central element(63) fixed. I want magic matrix such that sum along the diagonal of each page, sum of two main diagonals, and sum of all rows and column all equal to magic constant 315. Please I want the largest element on page 3, column 3
It is easy to change the code I gave you so as to preserve sums along the cross section diagonals and space diagonals and thereby preserve your "perfect cube" properties. Just replace the last line by:
A = A(p,p,p);
Now the maximum element, 125, will lie on the third page.
However, the central element is not preserved. It is my suspicion that it is impossible to 1) preserve all the sums in a perfect cube, 2) move the maximum element to page 3, and 3) preserve the central element (unless the maximum element is already at page 3.) As far as I am aware, property 3) is not one of those that are required of magic cubes. I see no mention of it on the Wikipedia site
http://en.wikipedia.org/wiki/Perfect_magic_cube
Why are you trying to preserve it? It looks to me as if it is an artifact of the method used to create Walter Trump's perfect magic cube.
Thank you for the answer. Now that the maximum element 125 is now on page 3, is it possible to move it to the edge (A(1,1,3);A(1,5,3),A(5,5,3),A(5,1,3). Getting it this way will be equivalent to what I want initially. Please help, this is very important in my edge detection research.
Can't you just assign the element you want with the max value (125)?
A(1,1,3) = 125;
or do you want to move/translate everything - all elements - with something like circshift()?
Maybe my question is not clear, What I want to do exactly is I want 125 at the corner of slice/page three at any location (A(1,1,3);A(1,5,3),A(5,5,3),A(5,1,3)) and still keep the properties of magic cube. What lines of code do I need to add to what Roger Stafford provided?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by