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:
vectorize this

Subject: vectorize this

From: Marios Karaoulis

Date: 31 Jan, 2012 19:57:12

Message: 1 of 5

Hello all,

Could you help me vectorize the following code? Ideally, I need to
remove the internal for loop.


num_mes=1890;
num_param=225;
Jx=rand(num_param,94221);
Jy=rand(num_param,94221);
Jz=rand(num_param,94221);

jac=zeros(num_mes,num_param);
ind_data=randi(num_mes,num_mes,4);

jtmp=randi(94221,500,num_param);


for i=1:num_mes

    A=ind_data(i,1);
    B=ind_data(i,2);
    M=ind_data(i,3);
    N=ind_data(i,4);

    JA = [Jx(A,:) ;Jy(A,:); Jz(A,:)].';
    JA = reshape(JA(mesh.T,:),[],4,3);

    JM = [Jx(M,:); Jy(M,:) ;Jz(M,:)].';
    JM = reshape(JM(mesh.T,:),[],4,3);

    JN = [Jx(N,:) ;Jy(N,:) ;Jz(N,:)].';
    JN = reshape(JN(mesh.T,:),[],4,3);

    JB = [Jx(B,:) ;Jy(B,:); Jz(B,:)].';
    JB = reshape(JB(mesh.T,:),[],4,3);

    J_all=dot(JA-JB,JM-JN,3);
    tmp_jac=mean(J_all,2);

    for k=1:num_param
         jtmp_this_iter =nonzeros( jtmp(:,k));
         jac(i,k)=sum(tmp_jac(jtmp_this_iter));
    end


end

Subject: vectorize this

From: Bruno Luong

Date: 1 Feb, 2012 11:55:10

Message: 2 of 5

Marios Karaoulis <marios.karaoulis@gmail.com> wrote in message <da1f7113-a999-4cb6-a905-8a0cae39138f@p13g2000yqd.googlegroups.com>...
> Hello all,
>
> Could you help me vectorize the following code? Ideally, I need to
> remove the internal for loop.
>
>
> num_mes=1890;
> num_param=225;
> Jx=rand(num_param,94221);
> Jy=rand(num_param,94221);
> Jz=rand(num_param,94221);
>
> jac=zeros(num_mes,num_param);
> ind_data=randi(num_mes,num_mes,4);
>
> jtmp=randi(94221,500,num_param);
>
>
> for i=1:num_mes
>
> A=ind_data(i,1);
> B=ind_data(i,2);
> M=ind_data(i,3);
> N=ind_data(i,4);
>
> JA = [Jx(A,:) ;Jy(A,:); Jz(A,:)].';
> JA = reshape(JA(mesh.T,:),[],4,3);
>
> JM = [Jx(M,:); Jy(M,:) ;Jz(M,:)].';
> JM = reshape(JM(mesh.T,:),[],4,3);
>
> JN = [Jx(N,:) ;Jy(N,:) ;Jz(N,:)].';
> JN = reshape(JN(mesh.T,:),[],4,3);
>
> JB = [Jx(B,:) ;Jy(B,:); Jz(B,:)].';
> JB = reshape(JB(mesh.T,:),[],4,3);
>
> J_all=dot(JA-JB,JM-JN,3);
> tmp_jac=mean(J_all,2);
>
> for k=1:num_param
> jtmp_this_iter =nonzeros( jtmp(:,k));
> jac(i,k)=sum(tmp_jac(jtmp_this_iter));
> end
>
>
> end

help ACCUMARRAY
help SPARSE

Bruno

Subject: vectorize this

From: Marios Karaoulis

Date: 6 Mar, 2012 08:03:13

Message: 3 of 5

> help ACCUMARRAY
> help SPARSE
>
> Bruno

Hey Bruno,
I searched on how the ACCUMARRAY works, but I can't figure t out.
Firstly, I would like to point out, that the indexes I get from the jtmp_this_iter
don't have the some length for every inner for loop (sometimes it is 16m some other times 24 etc).

How I would create the subs, needed for accumarray?

Subject: vectorize this

From: Roger Stafford

Date: 6 Mar, 2012 21:49:24

Message: 4 of 5

Marios Karaoulis <marios.karaoulis@gmail.com> wrote in message <da1f7113-a999-4cb6-a905-8a0cae39138f@p13g2000yqd.googlegroups.com>...
> .......
> num_mes=1890;
> num_param=225;
> Jx=rand(num_param,94221);
> Jy=rand(num_param,94221);
> Jz=rand(num_param,94221);
> .......
> ind_data=randi(num_mes,num_mes,4);
> ......
> A=ind_data(i,1);
> B=ind_data(i,2);
> M=ind_data(i,3);
> N=ind_data(i,4);
> ......
> JA = [Jx(A,:) ;Jy(A,:); Jz(A,:)].';
> ......
- - - - - - - - -
  You are using A, B, M, and N as row indices in Jx, Jy, and Jz which each have only 225 rows. Yet 'randi' can generate integers as high as 1890 (= 'num_mes') for these four variables. How do you avoid getting an error message about that (not to mention possibly meaningless results?)

Roger Stafford

Subject: vectorize this

From: Marios Karaoulis

Date: 7 Mar, 2012 00:02:24

Message: 5 of 5


> You are using A, B, M, and N as row indices in Jx, Jy, and Jz which each have only 225 rows. Yet 'randi' can generate integers as high as 1890 (= 'num_mes') for these four variables. How do you avoid getting an error message about that (not to mention possibly meaningless results?)
>
> Roger Stafford


None of the data I have are random. I just created some random matrices for you to understand the problem.
You are right thuoght, ind_data should be like this

ind_data = randi([1:num_param],num_mess,4);

Sorry about that.

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