Operations on 3d array without using for loop

조회 수: 6 (최근 30일)
SHRAVAN GARLAPATI
SHRAVAN GARLAPATI 2023년 9월 4일
편집: Bruno Luong 2023년 9월 5일
I am trying to write the following code without for loops
gf_k = gf(0:3,2); % gf is a Galois-field matlab function
gf_a = gf(a, 2); % gf is a Galois-field matlab function
for i = 1 : l
for j = 1 : h
for k = 0 : 3
c = gf_k.x(k+1) + gf_a.x(i,j); % As both gf_k and gf_a are GF(4) variables, Value of c is between any of 0,1,2,3
D(i,j,k+1) = B(i,j,k+1) + A(i,j,c+1);
end
end
end
I tried using sub2ind but I couldn't get right.
  댓글 수: 5
SHRAVAN GARLAPATI
SHRAVAN GARLAPATI 2023년 9월 4일
@William Rose and @Bruno Luong - Please take another look. I updated the code without func1 and func2. Removing for loops definetly improves speed. I had another 4-d for loop, I vectorized it and saw 4x speed-up. Hoping to do same for this 3-d for loop. But stuck.
Bruno Luong
Bruno Luong 2023년 9월 4일
This line of code is perhaps wrong
c = gf_k.x(k+1) + gf_a.x(i,j);
it adds two double and not GF(4) elements.

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

채택된 답변

Bruno Luong
Bruno Luong 2023년 9월 4일
편집: Bruno Luong 2023년 9월 4일
I don't have communication tbx, so I code this blindly
l = 300;
h = 400;
a = randi([0,3], l, h);
gf_k = gf(0:3,2);
gf_a = gf(a, 2);
A = rand(l,h,4);
B = rand(l,h,4);
% Addition table of GF(4)
[X,Y] = ndgrid(0:3);
GF4_addTable = (gf(X,2) + gf(Y,2));
GF4_addTable = GF4_addTable.x;
%GF4_addTable = [...
% 0 1 2 3;
% 1 0 3 2;
% 2 3 0 1;
% 3 2 1 0 ];
tic
gf_k_x = gf_k.x;
gf_a_x = gf_a.x;
gf_k_x = reshape(gf_k_x, [1 1 4]);
ilin = 1 + gf_a_x + 4*gf_k_x;
c = GF4_addTable(ilin); % gf_k_x + gf_a_x; in GF4
D = B + A((1:l)'+l*((0:h-1)+h*double(c)));
toc
Elapsed time is 0.023939 seconds.
  댓글 수: 2
SHRAVAN GARLAPATI
SHRAVAN GARLAPATI 2023년 9월 5일
편집: SHRAVAN GARLAPATI 2023년 9월 5일
@Bruno Luong - The code you provided works. Thanks. I see a speed-up around 3.5x. In my code, I already reduced the 3-for loops to 2-for loops. So, speed-up of 3.5x is w.r.t 2-for loops. w.r.t 3-for loops, I think the speedup your code provides is around 10x. I have the following questions:
ilin = 1 + gf_a_x + 4*gf_k_x;
gf_a_x is of dim(l,h) and gf_k_x is of dim(1,1,4). How can we add these two?
A((1:l)'+l*((0:h-1)+h*double(c)));
Is there a Matlab documentation page to understand this? Looks like you are using linear indexing. Is this correct?
Bruno Luong
Bruno Luong 2023년 9월 5일
편집: Bruno Luong 2023년 9월 5일
"gf_a_x is of dim(l,h) and gf_k_x is of dim(1,1,4). How can we add these two?"
A((1:l)'+l*((0:h-1)+h*double(c)));
... Looks like you are using linear indexing. Is this correct?
Correct. It does the work like sub2ind but with auto-expansion capability and without the overhead of verification for overflow that I don't need.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by