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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:11:18+00:00 2026-06-14T03:11:18+00:00

Here I have two matrix, one indicating cost and the other determines when to

  • 0

Here I have two matrix, one indicating cost and the other determines when to take into comparison.

cost =      [0.2 0.0 0.3; 0.4 0 0; 0.5 0 0];
available = [1   1   0  ; 1   0 0; 0   0 0];
available = logical(available);

I want to get the index of the min available element in the cost matrix, which in this case would compare 0.2, 0.0 and 0.4 and return the index of 0.0, which is (1, 2) or 4 in the cost matrix.

I tried

mul = cost .* available;     % Zero if not available, but I can't know if it is zero because cost is zero
mul(~mul) = nan;             % Set zero to be NaN
[minVal, minId] = min(mul)

This will help to get the min non-zero cost but if there exists zero elements which are available, it would be wrong.

So is there a better way to do so?

  • 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-14T03:11:19+00:00Added an answer on June 14, 2026 at 3:11 am

    Here are two possible solutions. Both essentially involve converting all non-available costs to Inf.

    %#Set up an example
    Cost =      [0.2 0 0.3; 0.4 0 0; 0.5 0 0];
    Available = [1   1   0; 1   0 0; 0   0 0];
    
    %#Transform non-available costs to Inf
    Cost(Available == 0) = Inf;
    
    %#Obtain indices using find
    [r, c] = find(Cost == min(min(Cost)))
    
    %#Obtain linear indices and convert using ind2sub
    [~, I1] = min(Cost(:));
    [r2, c2] = ind2sub(size(Cost), I1);
    

    Both solutions will only return the first minimum value in the instance that there is not a unique minimum. Also, the method will fail in the perverse case that all the available costs are Inf (but I guess you’ve got bigger problems if all your costs are infinite…).

    I’ve done a few speed tests, and the second method is definitely faster, no matter what the dimensions of Cost, so should be strictly preferred. Also, if you only want linear indices and not subscript indices then you can of course drop the call to ind2sub. However, this doesn’t give you huge savings in efficiency, so if there is a preference for subscript indices then you should use them.

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

Sidebar

Related Questions

Let's say I have two data sets, one with y-range [min0:max0] and the other
What is the issue with this code? Here we have two files: classA.h and
My script is here : jsfiddle i have two attributes in my form that
I have two questions here. How can I show the header of the pop
I have two functions here function Preloader() {} Preloader.prototype = { init:function() { //
I have two tables as shown here . I am trying to fetch name
Here is the problem: I have two columns in a table that, for each
Here is my scenario: I have two MySQL tables: Categories (columns: id, category) Items
I have two select boxes with dates. See here the jsfiddle: http://jsfiddle.net/egrba/ The people
I have two tables: entitytype and project . Here are the create table statements:

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.