Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7883841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:38:31+00:00 2026-06-03T04:38:31+00:00

Here is piece of Matlab code. It works very slow. Is there any way

  • 0

Here is piece of Matlab code. It works very slow. Is there any way to make it work faster? I cant figure out the way to vectorize it.Maybe it can be written like some kind of filter ?

...
for uu=2:length(x)-2;
    for vv= 2:length(y)-2;

     P1=[x(uu+1) y(vv) temp(uu+1,vv)];
     P2=[x(uu) y(vv+1) temp(uu,vv+1)];
     P3=[x(uu-1),y(vv) temp(uu-1,vv)];
     P4=[x(uu) y(vv-1) temp(uu,vv-1)];


     cr=cross((P1-P3),(P2-P4));

     cr=cr/norm(cr);
     theta=acos(dot([0,0,1],px))*180/pi;
     ...
 end
end
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:38:32+00:00Added an answer on June 3, 2026 at 4:38 am

    To vectorize this nested loop, you need to reshape your data properly. The cross product can be performed along a certain dimension, so all you really need to do is to reshape P1 … P4 into vectors containing your data well ordered.

    Notice that you have a “cross-shaped” kernel for your operation. P1 could be the bottom part, P3 the top part, P2 the right part and P4 the left part. Assuming that the vectors x and y are only the coordinates of sampled points (temp), each of those vectors could be represented like this :

    [x,y] = meshgrid(1:size(temps,1),1:size(temp,2)); % Create a sampling grid or replicate the one you have
    
    tmp1 = x(3:end,2:end-1);
    tmp2 = y(3:end,2:end-1);
    tmp3 = temp(3:end,2:end-1);
    P1 = [tmp1(:), tmp2(:), tmp3(:)] % Vectorization
    
    tmp1 = x(2:end-1,3:end);
    tmp2 = y(2:end-1,3:end);
    tmp3 = temp(2:end-1,3:end);
    P2 = [tmp1(:), tmp2(:), tmp3(:)];
    
    tmp1 = x(1:end-2,2:end-1);
    tmp2 = y(1:end-2,2:end-1);
    tmp3 = temp(1:end-2,2:end-1);
    P3 = [tmp1(:), tmp2(:), tmp3(:)];
    
    tmp1 = x(2:end-1,1:end-2);
    tmp2 = y(2:end-1,1:end-2);
    tmp3 = temp(2:end-1,1:end-2);
    P4 = [tmp1(:), tmp2(:), tmp3(:)];
    
    V1 = P1 - P3;
    V2 = P2 - P4;
    
    CR = cross(V1,V2);
    NRM = (CR(:,1).^2 + CR(:,2).^2 + CR(:,3).^2).^0.5; % norm(X) cannot be vectorized
    CR = CR./NRM;
    

    Theta doesn’t seem to be dependent of any variables in your loop, but acos(X) and dot(v1,v2) can be used with vectorized data, same as cross(v1,v2).

    I doubt that you could get any performance boost from decomposing the cross product function in its cofactor expansion, or by trying to implement it as some kind of non-linear filter. If it’s still too slow, you should have a look at the Parallel toolbox.

    Hope this helps !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my piece of code in jquery actuall I want in such way
Here's a piece of code I copied from http://www.schillmania.com/content/projects/javascript-animation-1/demo/ Very simple JS animation: function
here is a piece of my code, but in this way, when I push
Here's a piece of code: $obj = new myClass(); $obj->{$_GET[func]}($_GET[param]) It works based on
Here is my piece of code that creates buttons programmatically: NSArray *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@Cover_0.png],
Here a small piece of code for testing and explain the problem. I have
Here I have piece of code, showing example of how I want to update
Here is a piece of my code $file=Files::model()->findByPk($id); if($file == null) { throw new
Here is the piece of code that I have used for Java 5.0 TreeSet<Integer>
Here's a piece of code from the xss_clean method of the Input_Core class of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.