Asked by Amit
on 4 Nov 2012

I am looking for a single-line matlab code to solve the following problem:

There are five consultants, and five projects to be done. Any consultants can carry out any the project, but at a different cost to the company. The cost matrix is as follows:

The business manager is trying to allocate one project each to the five sonsultants such that the overall cost to the company is the minimum.

Obviously, there are 5! (=120) possibilities, and total costs can be calculated, and combination with minimum cost can be selected.

The challenge is to do it in single line of matlab code, however complex.

For your ease: costs=[24 10 21 11 16; 14 22 10 15 15; 15 17 12 20 14; 11 19 14 13 13; 23 13 13 18 12]; Thank you.

Answer by Amit
on 6 Nov 2012

Accepted Answer

I got a notification that José-Luis posted an answer. I read it first but strangely I can't see it here. This is what José-Luis posted, and it works like a charm:

costs=[24 10 21 11 16; 14 22 10 15 15; 15 17 12 20 14; 11 19 14 13 13; 23 13 13 18 12];

min(sum(costs(bsxfun(@plus,perms(1:5),0:5:20)),2))

Thanks José-Luis.

Sign in to comment.

Answer by Matt J
on 4 Nov 2012

Edited by Matt J
on 4 Nov 2012

The PERMS command.

Amit
on 5 Nov 2012

Matt J
on 5 Nov 2012

No, it's not all that you require, but it's the main thing that you require. I don't see why you think you need to shuffle the matrix and sum the diagonal.

As a hint, here are a few other commands that might be useful: SUB2IND, REPMAT

Sign in to comment.

Answer by Matt Fig
on 4 Nov 2012

Amit
on 5 Nov 2012

Hi Matt Fig, yes, the correct answer is 59. Still trying to achieve this one with a single line code. So need more insights.

SUBSREF... to be honest, I do not understand it yet.

Best.

Matt J
on 5 Nov 2012

If the goal is just the minimum cost, you probably don't need SUBSREF.

Walter Roberson
on 5 Nov 2012

Remember, one line, not one expression.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.