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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:05:00+00:00 2026-06-15T23:05:00+00:00

When I answered this question, my answer started with: For starters, you can get

  • 0

When I answered this question, my answer started with: “For starters, you can get rid of all the if-statements by storing all the names in a cell.”

The “For starters” bit was because I thought that I could add an edit with a vectorized solution. But when I tried to do so I ran into trouble vectorizing the use of mrdivide (b/a).

My question (marked below in the code) is if it is possible to solve b(z,:)/a(z,:) without using a loop. In other words, to solve b/a independently for each row of the matrices.

person = [98   206    35   114;
          60   206    28    52;
         100   210    31   116;
          69   217    26    35;
          88   213    42   100];

person1 = [93 208 34 107];

allNames = {'Cameron'; 'David'; 'Mike'; 'Bill'; 'Joe'};

n = 5;
a = max(person,repmat(person1,n,1));
b = min(person,repmat(person1,n,1));

for z = 1:5
    percent_error = b(z,:)/a(z,:);          %// Here is my question
    if percent_error >= 0.85
        disp(['Match, its ', allNames{z} ,'!'])
    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-15T23:05:01+00:00Added an answer on June 15, 2026 at 11:05 pm

    You can indeed eliminate the loop by vectorizing the operation. The trick is working with diagonal block matrices. Each block is matrix with only one row (each time a different row). After you create such a block matrix for a and for b, you can use mrdivide:

    % # Without loop
    tic
    A = zeros(size(a) * size(a, 1));
    B = zeros(size(b) * size(b, 1));
    V = ones(size(a, 2), 1) * (1:size(a, 1));
    idx = (0:size(A, 1):numel(A) - 1) + (V(:)' - 1) * size(a, 1) + 1;
    A(idx) = a';
    B(idx) = b';
    X = diag(B / A);
    percent_error1 = X(1:size(a, 1):end);
    toc
    
    % # With loop
    tic
    percent_error2 = zeros(5, 1);
    for z = 1:5
        percent_error2(z) = b(z,:) / a(z,:);
    end
    toc
    

    The result is:

    Elapsed time is 0.000160 seconds.
    Elapsed time is 0.000048 seconds.
    
    percent_error1 =
        0.9741
        0.8516
        0.9670
        0.8221
        0.9611
    
    percent_error2 =
        0.9741
        0.8516
        0.9670
        0.8221
        0.9611
    

    Note that this is one of those cases where matrix division of large arrays takes longer than a for loop.

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

Sidebar

Related Questions

Sorry if this question has already been answered but I can't find an answer.
Warning, this is a bit recursive ;) I answered this question: Python:How can i
Sorry if this question is answered already, but I didn't find a suitable answer.
I know this question can be answered by searching in google. But I have
I recently answered this question how-to-call-user-defined-function-in-order-to-use-with-select-group-by-order-by My answer was to use an inline view
I have seen this question answered here, but unfortunately the answer does not work.
First of all, I am sorry that this is not a 'question that can
@robot_sherrick answered me this question , this is a follow-up question for his answer.
UPDATE (spoiler): This question is answered (see David Carlisle answere below) and it looks
Edit : This question has already been asked and answered, and I apparently am

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.