For Loop in 3D Array

조회 수: 10 (최근 30일)
MarshallSc
MarshallSc 2022년 5월 31일
댓글: MarshallSc 2022년 5월 31일
For 2D arrays, the operation that I want to execute is:
a = rand(3,3);
for i = 1:numel(a)
for j = 1:numel(a)
out(i,j) = (a(i) - a(j)) / (a(i) + a(j));
end
end
Which will turn the out into a skew-symmetric matrix. Now I want to perform the same operation instead the matrix is in 3D, how can I do that? The speed is important since my original matrix is (100,100,726). Thanks!

채택된 답변

Dyuman Joshi
Dyuman Joshi 2022년 5월 31일
편집: Dyuman Joshi 2022년 5월 31일
a = rand(3,3,4);
for k=1:size(a,3)
y=a(:,:,k);
for i=1:size(y,1)
for j=1:size(y,2)
out(i,j,k)=(y(i) - y(j))/(y(i) + y(j));
end
end
end
out
out =
out(:,:,1) = 0 0.6498 -0.1418 -0.6498 0 -0.7248 0.1418 0.7248 0 out(:,:,2) = 0 -0.6166 -0.5396 0.6166 0 0.1154 0.5396 -0.1154 0 out(:,:,3) = 0 0.1018 0.1400 -0.1018 0 0.0387 -0.1400 -0.0387 0 out(:,:,4) = 0 0.2123 0.1857 -0.2123 0 -0.0277 -0.1857 0.0277 0
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2022년 5월 31일
It will be skew-symmetric matrix. I have made an edit, take a look at it again.
MarshallSc
MarshallSc 2022년 5월 31일
Thanks, yeah, changing the i and j indices from 1:size(y,1) to numel(y) would do the trick. Thanks again!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by