필터 지우기
필터 지우기

looking for convenient way to extract matrix from 1*n*n

조회 수: 3 (최근 30일)
Yu Li
Yu Li 2023년 6월 4일
댓글: Yu Li 2023년 6월 4일
Hi:
I have a 3*3*3 matrix, and I want to extract the data after its summation.
for example:
A=zeros(5,5,5)
B=sum(A,1);
now B is a 1*5*5 matrix, I want to assign it to a 5*5 matrix C but could not find a way, so looking for help here.
I tried
C=B(1,:,:)
but it does not work.
Thanks!

채택된 답변

Ayush
Ayush 2023년 6월 4일
Hi YuLi,
You can use squeeze function here:
A=zeros(5,5,5);
size(A) ;%5x5x5
B=sum(A,1);
size(B) %1x5x5
C=squeeze(B);
size(C) %5x5
squeeze function removes all singleton dimensions. You can read more about them here.
It might interest you : Adding extra dimension
Now as, here we are squeezing the dimension, we can also add another dimension to already existing matrix.
A=randi([-10,10],4,4);
size(A) %4x4
C(:,:,2)=C;
size(C) %4x4x2
Hope it helps.

추가 답변 (2개)

David Goodmanson
David Goodmanson 2023년 6월 4일
편집: David Goodmanson 2023년 6월 4일
Hi YL,
A=zeros(5,5,5)
B=squeeze(sum(A,1));
Squeeze removes all singleton dimensions.

Walter Roberson
Walter Roberson 2023년 6월 4일
A = zeros(5,5,5)
A =
A(:,:,1) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A(:,:,2) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A(:,:,3) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A(:,:,5) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B = permute(sum(A,1), [2 3 1]);
size(B)
ans = 1×2
5 5
squeeze() is a convenience function that ends up invoking permute(). squeeze() will not always give you the result you might expect:
C = squeeze(ones(1,1,5)), size(C)
C = 5×1
1 1 1 1 1
ans = 1×2
5 1
Notice that squeeze() removed all of the singular dimensions, not just the first singular dimension.
  댓글 수: 1
Yu Li
Yu Li 2023년 6월 4일
thank you Walter, Ayush's answer come first so I accepted his. but your answer extend my understanding, thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by