Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
JazzMusic
JazzMusic 2016년 11월 29일
마감: MATLAB Answer Bot 2021년 8월 20일
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!

답변 (2개)

Alexandra Harkai
Alexandra Harkai 2016년 11월 29일
  댓글 수: 1
JazzMusic
JazzMusic 2016년 11월 30일
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 2016년 11월 30일
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]);

태그

Community Treasure Hunt

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

Start Hunting!

Translated by