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 8349737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:06:00+00:00 2026-06-09T08:06:00+00:00

I encountered a very slow if statement response using cuda\jacket in matlab. (5 sec

  • 0

I encountered a very slow if statement response using cuda\jacket in matlab. (5 sec vs 0.02 sec for the same code that finds local maxima, using a simple for loop and an if condition)

Being new to GPU programming, I went reading and when I saw a previous matlab if statements with CUDA SO discussion, I felt something is missing.
You don’t need to use cuda to know that it is better to vectorized your code. However, there are cases where you will need to use an if statement anyway.
For example, I’d like to find whether a pixel of a 2D image (say m(a,b)) is the the local maximum of its 8 nearest neighbors. In matlab, an easy way to do that is by using 8 logical conditions on an if statement:

if m(a,b)>m(a-1,b-1) & m(a,b)>(a,b-1) & m(a,b)>(a+1,b-1) & … etc on all nearest neighbors

I’d appreciate if you have an idea how to resolve (or vectorize) this…

  • 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-09T08:06:02+00:00Added an answer on June 9, 2026 at 8:06 am

    The problem with using multiple “if” statement (or any other conditional statement) is that for each the statements, the result is copied from gpu to host and this can be costly.

    The simplest way is to vectorize in the following manner.

    window = m(a-1:a+1, b-1:b+1);
    if all(window(:) <= m(a,b))
    % do something
    end
    

    This can be further optimized if you can show what the if / else conditions are doing. i.e. please post the if/else code to see if other optimizations are available (i.e look at possible ways to remove if condition entirely).

    EDIT

    With new information, here is what can be done.

    for j = 1:length(y)
     a = x(j);
     b = y(j);
     window = d(a-1:a+1, b-1:b+1);
     condition = all(window(:) <= d(a,b));
     M(a, b) = condition + ~condition * M(a,b);
    end
    

    You can use gfor loop to make it even faster.

    gfor j = 1:length(y)
     a = x(j);
     b = y(j);
     window = d(a-1:a+1, b-1:b+1);
     condition = all(window(:) <= d(a,b));
     M(a, b) = condition + ~condition * M(a,b);
    gend
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an interesting problem that I encountered in a programming competition: Problem statement:
I have encountered a very strange problem in using jQuery load() . I used
I've encountered a very strange issue using JMenuBar where navigating between two JMenus results
I encountered a very annoying change for me from .NET3.5 to .NET4.0. When using
I encountered a problem when running some old code that was handed down to
I've recently encountered a very rarely reproducible bug. If I cast a pointer that
I have encountered a very weird behavior while using SimpleDateFormat for parsing a string
I have encountered a very weird problem with fancybox modal that whenever my website
I encountered a very strange behavior in Python, a behavior that is not consistent.
I encountered a very strange behavior when i m trying to insert a NSMutableDictonary

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.