What does permute actually produce?

4 次查看(过去 30 天)
Trying some calculations earlier, I have realised permute does not do what I thought it did. To clarify I did this
% code
test = rand(4,4,4,4) + rand(4,4,4,4)*1i;
test2a = zeros(4,4,4,4); test2b = zeros(4,4,4,4);
for a = 1:4
for b = 1:4
for c = 1:4
for d = 1:4
test2a(a,b,c,d) = test(a,b,d,c);
test2b(a,b,c,d) = test(b,a,c,d);
end
end
end
end
test3a = permute(test,[1,2,4,3]);
test3b = permute(test,[2,1,3,4]);
m1=max(test2a(:)-test3a(:))
m2=max(test2b(:)-test3b(:))
test2a = zeros(4,4,4,4); test2b = zeros(4,4,4,4);
for a = 1:4
for b = 1:4
for c = 1:4
for d = 1:4
test2a(a,b,c,d) = test(d,b,a,c);
test2b(a,b,c,d) = test(c,a,b,d);
end
end
end
end
test3a = permute(test,[4,2,1,3]);
test3b = permute(test,[3,1,2,4]);
m3=max(test2a(:)-test3a(:))
m4=max(test2b(:)-test3b(:))
m1 =
0
m2 =
0
m3 =
0.9525 + 0.5183i
m4 =
0.9293 - 0.7266i
clearly the first is the same but the latter is different. Is there some way to create the second variables (of form test2(a,b,c,d) == test(d,b,a,c) which involve two permutations ) just using permute on test?
  1 个评论
David H
David H 2014-11-20
I realise reading this again I haven't really stated what I want to do very clearly.
If I want some variable satisfying var2(a,b,c,d) == var1(d,b,a,c) for any a b c d (up to the size of the variable) how do I produce this variable efficiently from var1?

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2014-11-20
Isn't it
var2 = permute(var1,[3 2 4 1]);
?
  3 个评论
the cyclist
the cyclist 2014-11-20
Here is the reasoning:
  • The former dimension 3 is now dimension 1.
  • The former dimension 2 is now dimension 2.
  • The former dimension 4 is now dimension 3.
  • The former dimension 1 is now dimension 4.
So, you see that new dimension [1 2 3 4] is old dimension [3 2 4 1].
That latter vector is the one you needed.
David H
David H 2014-11-21
编辑:David H 2014-11-21
Ah yes I was thinking on the lines of:
  • The new dimension 1 is dimension 4,
  • The new dimension 2 is dimension 2,
  • new dimension 3 is dimension 1 etc
rather than this way round, which is of course not the values you call permute with!
Thanks for the clarification, I realise it was a fairly nieve mistake.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by