Author Topic: Can this be written in Matlab notation without loop?  (Read 763 times)

Offline matthias77

  • Newbie
  • *
  • Posts: 1
  • Matlab Forum
    • View Profile
Can this be written in Matlab notation without loop?
« on: December 06, 2010, 04:18:56 PM »
Hi fellow Matlab coders,

I wonder if there is any way to get rid of at least the inner loop by using matrix notation (what I mean by using matrix notation is like eg. 'for i = j, A(i) = B(i) -1, end' can be replaced by 'A(j) = B(j) - 1' and similar) ?

A and B are constructed in a way that elements in 'data' that are written are not read

Code: [Select]
for i = 1:size(A,1)
 s = 0;
 for j = 1:size(B,2)
   ind = A(i,:)' + B(:,j)
   s = s + data(ind(1),ind(2));
 end
 data(A(i,1),A(i,2)) = data(A(i,1),A(i,2)) + s;
end

What the code does is a stencil (here with values allways 1) looping over two-dimensional 'data'.
B gives shape of the stencil, A the elements in 'data' to be processed.

I am thinking if converting to a one-dimensional array for data could help/make sence?

Thanks a lot for any help,

Matthias

Matlab and SimuLink Development Forum

Can this be written in Matlab notation without loop?
« on: December 06, 2010, 04:18:56 PM »