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

  • Home
  • SEARCH
  • 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 6801329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:05:13+00:00 2026-05-26T19:05:13+00:00

I need to vectorize the following code: a = [1 2 3 2 3

  • 0

I need to vectorize the following code:

a = [1 2 3 2 3 1];
b = [1 2 3];

for i = 1:length(a)
    for j = 1:length(b)
        r(i, j) = (a(i) == b(j));
    end
end

The output r should be a logical array:

 1     0     0
 0     1     0
 0     0     1
 0     1     0
 0     0     1
 1     0     0

The closest I can get is:

for j = 1:length(b)
    r(:, j) = (a == b(j));
end

Iterating through the shorter vector is obviously more efficient as it generates fewer for iterations. The correct solution should have no for-loops whatsoever.

Is this possible in MATLAB/Octave?

  • 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-05-26T19:05:13+00:00Added an answer on May 26, 2026 at 7:05 pm

    Here’s a simple solution using bsxfun.

    bsxfun(@eq,b,a')
    
    ans =
    
         1     0     0
         0     1     0
         0     0     1
         0     1     0
         0     0     1
         1     0     0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to vectorise code like the following? length(x) <- 100; x[1] <-
Need to locate the following pattern: The letter I followed by a space then
need advice about the following with the following ksh script I actually copy file1
Need help with reading special characters within my VB code. ASCII code Char(34) =
Need to turn javascript (ajax) vote script into reusable (dynamic) script so it can
I need your help in solving the following problem. Column1 shows a grouping by
To use code I have written for performing calculations, I need to read in
Need help with copying array object to a temp array object using a for
I need help with the Pythonic looping overhead of the following problem: I'm writing
I need to efficiently swap the byte order of an array during copying into

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.