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