Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
efficient programming

Subject: efficient programming

From: Marios Karaoulis

Date: 10 May, 2010 22:50:10

Message: 1 of 7

Hi,
I have this problem

k5=rand(10,10);
index=[1 5 7];

I want to zero all elements in matrix k5, that are in line 1, 5 and 7.
This is how I do it


for i=1:3
    for j=1:10
        k5(index(i),j)=0;
    end
end

is there a way to avoid the double for loop, like using indexes?
Thanks

Subject: efficient programming

From: ImageAnalyst

Date: 10 May, 2010 23:01:36

Message: 2 of 7

k5(index,:) = 0

Subject: efficient programming

From: Marios Karaoulis

Date: 10 May, 2010 23:09:20

Message: 3 of 7

On May 10, 5:01 pm, ImageAnalyst <imageanal...@mailinator.com> wrote:
> k5(index,:) = 0

and if you have double indexes?
k5=rand(10,10);
index=[1 5 7];
index2=[2 4 6]'


for i=1:3
    for j=3
        k5(index(i),index2(j))=0;
    end
end

Subject: efficient programming

From: Roger Stafford

Date: 10 May, 2010 23:35:05

Message: 4 of 7

Marios Karaoulis <marios.karaoulis@gmail.com> wrote in message <852a0217-6000-4eb7-8084-67dd877f3d1f@j36g2000prj.googlegroups.com>...
> On May 10, 5:01 pm, ImageAnalyst <imageanal...@mailinator.com> wrote:
> > k5(index,:) = 0
>
> and if you have double indexes?
> k5=rand(10,10);
> index=[1 5 7];
> index2=[2 4 6]'
>
>
> for i=1:3
> for j=3
> k5(index(i),index2(j))=0;
> end
> end

  Then do

 k5(index,index2) = 0;

Roger Stafford

Subject: efficient programming

From: Marios Karaoulis

Date: 13 May, 2010 21:46:19

Message: 5 of 7

I tried and succeed making some of my function more efficient. But I can't find a way to handle this function.
I need to make some sums in a triple for loop.
consider this.

num_mes=43;
num_param=104;
% pa and pm are a INTEGER matrices dimension 1Xnum_mes
pa=[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,7,7,7,7,8,8,8,9,9,9;];
pm=[3,4,5,6,7,4,5,6,7,8,1,5,6,7,8,9,1,2,6,7,8,9,1,2,3,7,8,9,2,3,4,8,9,3,4,5,9,4,5,6,5,6,7;];

% icon is a INTEGER matrix in 3X2470 dimensions
icon=randi([1 1303],3,2470);
%aaa is a float matrix with 10X1303 dimensions
aaa=rand([10,1303]);
%x_flux y_flux is a float matrix with 10X2470 dimensions
x_flux=rand([10,2470]);
y_flux=rand([10,2470]);

%width_param is a INTEGER mX1 matrix, that shows how many times the
%internal loop must be calculated.
width_param=randi([1 50],num_param,1);

for mes=1:num_mes
  for m=1:num_param
      for k=1:width_param(m)
         
                      l=param(m,k);
         a1=icon(1,l);
                      a2=icon(2,l);
                      a3=icon(3,l);
                      area=area_elm(l);

i=pa(mes);
j=pm(mes);

s1=aaa(i,a1)+aaa(i,a2)+aaa(i,a3);
s2=aaa(j,a1)+aaa(j,a2)+aaa(j,a3);
sum=2*(aaa(i,a1)*aaa(j,a1)+aaa(i,a2)*aaa(j,a2)+aaa(i,a3)*aaa(j,a3))+s1*s2;
       
sum=area*sum*k_absc(kit)*k_absc(kit)/(12*prop(l)*prop(l));

jam=x_flux(i,l)*x_flux(j,l)+y_flux(i,l)*y_flux(j,l)+sum;

                      jactmp=jactmp+jam;
                

      end
trans_jac2(mes,m)=jactmp;
      jactmp=0;
  end
end


Any ideas?

Subject: efficient programming

From: Marios Karaoulis

Date: 13 May, 2010 21:48:04

Message: 6 of 7

How can i optimize this triple for loop?

num_mes=43;
num_param=104;
% pa and pm are a INTEGER matrices dimension 1Xnum_mes
pa=[1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,7,7,7,7,8,8,8,9,9,9;];
pm=[3,4,5,6,7,4,5,6,7,8,1,5,6,7,8,9,1,2,6,7,8,9,1,2,3,7,8,9,2,3,4,8,9,3,4,5,9,4,5,6,5,6,7;];

% icon is a INTEGER matrix in 3X2470 dimensions
icon=randi([1 1303],3,2470);
%aaa is a float matrix with 10X1303 dimensions
aaa=rand([10,1303]);
%x_flux y_flux is a float matrix with 10X2470 dimensions
x_flux=rand([10,2470]);
y_flux=rand([10,2470]);

%width_param is a INTEGER mX1 matrix, that shows how many times the
%internal loop must be calculated.
width_param=randi([1 50],num_param,1);

for mes=1:num_mes
  for m=1:num_param
      for k=1:width_param(m)
         
                      l=param(m,k);
         a1=icon(1,l);
                      a2=icon(2,l);
                      a3=icon(3,l);
                      area=area_elm(l);

i=pa(mes);
j=pm(mes);

s1=aaa(i,a1)+aaa(i,a2)+aaa(i,a3);
s2=aaa(j,a1)+aaa(j,a2)+aaa(j,a3);
sum=2*(aaa(i,a1)*aaa(j,a1)+aaa(i,a2)*aaa(j,a2)+aaa(i,a3)*aaa(j,a3))+s1*s2;
       
sum=area*sum*k_absc(kit)*k_absc(kit)/(12*prop(l)*prop(l));

jam=x_flux(i,l)*x_flux(j,l)+y_flux(i,l)*y_flux(j,l)+sum;

                      jactmp=jactmp+jam;
                

      end
trans_jac2(mes,m)=jactmp;
      jactmp=0;
  end
end

Subject: efficient programming

From: Marios Karaoulis

Date: 13 May, 2010 22:02:20

Message: 7 of 7

also

prop=rand([1,2470]);
area_elm=rand([1,2470]);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us