Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How to creat a four dimentional (from a vector) matrix and reset it's 'lower triangle'

1 vue (au cours des 30 derniers jours)
JazzMusic
JazzMusic le 29 Nov 2016
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hi,
I am trying to get a 4 dimentional matrix out of a vector and then reset it's 'lower triangel'. for example, if my original vector is two dimentional: A = [1 2]' then I would like my initial matrix to be:
C(:,:,1,1) = [1*1*1*1 1*1*1*2 ; 1*1*2*1 1*1*2*2] = [ 1 2 ; 2 4]
C(:,:,2,1) = [2*1*1*1 2*1*1*2 ; 2*1*2*1 2*1*2*2] = [ 2 4 ; 4 8]
C(:,:,1,2) = [1*2*1*1 1*2*1*2 ; 1*2*2*1 1*2*2*2] = [ 2 4 ; 4 8]
C(:,:,2,2) = [2*2*1*1 2*2*1*2 ; 2*2*2*1 2*2*2*2] = [ 4 8 ; 8 16]
So C is:
C(:,:,1,1) = [ 1 2 ; 2 4] C(:,:,2,1) = [ 2 4 ; 4 8]
C(:,:,1,2) = [ 2 4 ; 4 8] C(:,:,2,2) = [ 4 8 ; 8 16]
and after reset I would like it to be:
C(:,:,1,1) = [ 1 2 ; 2 4] C(:,:,2,1) = [ 0 0 ; 0 0]
C(:,:,1,2) = [ 0 0 ; 4 8] C(:,:,2,2) = [ 0 0 ; 8 16]
shotrly, I want no rows repetitions.
I tried the following code:
A = [1 2]';
C = bsxfun(@times, permute(C, [4 3 2 1]), C*C');
disp('C before reset is:');
disp(C);
for k = 2:size(C, 4)
C(1:k-1,:,k) = 0;
end
disp('C after reset is:');
disp(C);
disp('The size of C is:');
disp(size(C));
But the output is:
BB before reset is:
(:,:,1,1) =
1 2
2 4
(:,:,1,2) =
2 4
4 8
C after reset is:
(:,:,1,1) =
1 2
2 4
(:,:,1,2) =
0 0
4 8
The size of BB is:
2 2 1 2
What did I miss? I think I don't understand what is behind the line:
C = bsxfun(@times, permute(C, [4 3 2 1]), C*C');
what is the meaning of each number in the row [4 3 2 1]?
Thanks!

Réponses (2)

Alexandra Harkai
Alexandra Harkai le 29 Nov 2016
  1 commentaire
JazzMusic
JazzMusic le 30 Nov 2016
I (obviosley) looked there before asking this question... but they only talk about 2 dimentional permutations. of course, [2 1] is like doinge transpose, but this doesn't help mh in higher dimentions like: permute([1 2]',[4 3 2 1]) I can't figure what does the permute function is doing in such situations...
thanks anyway

Guillaume
Guillaume le 30 Nov 2016
I've not tried to understand exactly what you're doing but this produces the right result:
A = [1 2];
k1 = kron(A.', A);
B = reshape(kron(k1, k1), [], 2); %to be reshape later
%duplicate only 1st instance of each row
C = zeros(size(B));
[~, irows] = unique(B, 'rows');
C(irows, :) = B(irows, :);
%reshape into final matrix
C = permute(reshape(C.', [2 2 2 2]), [2 1 3 4]);

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by