Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix manipulation

Subject: Matrix manipulation

From: Milos Milenkovic

Date: 16 Jun, 2009 19:00:20

Message: 1 of 4

A matrix containing all possible solutions of a problem is given as follows:
> m=4
> t=5
> t=[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0]
> We have to find a minimum sum of all nonzero elements in matrix under the conditions that in each row and column can be just one element.
> For this example the steps are:
> 1.Begin from first row and find minimum - 360
> 2.Continue at second row and find minimum - 30
> 3.In the third row minimum is 90 but because in that column has already chosen minimum pass on the next cell and that is 190.
> 4. In the fourth row there is no nonzero elements so don't consider
> 4.Sum all chosen numbers.
>
> Thank you very much!
> You are great all!

I don't know where and how to define constraint 3. to get the wanted solution

t =[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0];
       
proc1Matrix = zeros(size(t));
proc2Matrix = zeros(size(t));
for i = 1:size(t, 1);
    currRow = t(i, :);
    nonZeroElems = nonzeros(currRow);
    minNonZeroElem = min(nonZeroElems);
    if ~isempty(minNonZeroElem)
        minElement = find(currRow == minNonZeroElem);
        proc1Matrix(i, minElement) = minNonZeroElem
    end
end
Z=sum(sum(proc1Matrix))

Subject: Matrix manipulation

From: Nathan

Date: 16 Jun, 2009 19:36:42

Message: 2 of 4

On Jun 16, 12:00 pm, "Milos Milenkovic" <m.milenko...@mathworks.com>
wrote:
> A matrix containing all possible solutions of a problem is given as follows:
>
> > m=4
> > t=5
> > t=[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0]
> > We have to find a minimum sum of all nonzero elements in matrix under the conditions that in each row and column can be just one element.
> > For this example the steps are:
> > 1.Begin from first row and find minimum - 360
> > 2.Continue at second row and find minimum - 30
> > 3.In the third row minimum is 90 but because in that column has already chosen minimum pass on the next cell and that is 190.
> > 4. In the fourth row there is no nonzero elements so don't consider
> > 4.Sum all chosen numbers.
>
> > Thank you very much!
> > You are great all!
>
> I don't know where and how to define constraint 3. to get the wanted solution
>
> t =[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0];
>
> proc1Matrix = zeros(size(t));
> proc2Matrix = zeros(size(t));
> for i = 1:size(t, 1);
>     currRow = t(i, :);
>     nonZeroElems = nonzeros(currRow);
>     minNonZeroElem = min(nonZeroElems);
>     if ~isempty(minNonZeroElem)
>         minElement = find(currRow == minNonZeroElem);
>         proc1Matrix(i, minElement) = minNonZeroElem
>     end
> end
> Z=sum(sum(proc1Matrix))

t =[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0];

proc1Matrix = zeros(size(t));
proc2Matrix = zeros(size(t));
for i = 1:size(t, 1);
    currRow = t(i, :);
    nonZeroElems = nonzeros(currRow);
    minNonZeroElem = min(nonZeroElems);
    if ~isempty(minNonZeroElem)
        minElement = find(currRow == minNonZeroElem);
        if length(find(proc1Matrix(:,mod(i,5)+1))) > 0
           t(i,minElement) = 0;
           currRow = t(i,:);
           nonZeroElems = nonzeros(currRow);
           minNonZeroElem = min(nonZeroElems);
           minElement = find(currRow == minNonZeroElem);
        end
        proc1Matrix(i, minElement) = minNonZeroElem
    end
end
Z=sum(sum(proc1Matrix))

Here's one way.
Just check to see if the column the new minimum is in already has a
value, if so, go to the next minimum value in that row.

Subject: Matrix manipulation

From: Milos Milenkovic

Date: 16 Jun, 2009 19:42:02

Message: 3 of 4

Nathan <ngreco32@gmail.com> wrote in message <6d71e4d9-dd3e-4d3b-99c2-2ee45a4c6799@v35g2000pro.googlegroups.com>...
> On Jun 16, 12:00?pm, "Milos Milenkovic" <m.milenko...@mathworks.com>
> wrote:
> > A matrix containing all possible solutions of a problem is given as follows:
> >
> > > m=4
> > > t=5
> > > t=[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0]
> > > We have to find a minimum sum of all nonzero elements in matrix under the conditions that in each row and column can be just one element.
> > > For this example the steps are:
> > > 1.Begin from first row and find minimum - 360
> > > 2.Continue at second row and find minimum - 30
> > > 3.In the third row minimum is 90 but because in that column has already chosen minimum pass on the next cell and that is 190.
> > > 4. In the fourth row there is no nonzero elements so don't consider
> > > 4.Sum all chosen numbers.
> >
> > > Thank you very much!
> > > You are great all!
> >
> > I don't know where and how to define constraint 3. to get the wanted solution
> >
> > t =[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0];
> >
> > proc1Matrix = zeros(size(t));
> > proc2Matrix = zeros(size(t));
> > for i = 1:size(t, 1);
> > ? ? currRow = t(i, :);
> > ? ? nonZeroElems = nonzeros(currRow);
> > ? ? minNonZeroElem = min(nonZeroElems);
> > ? ? if ~isempty(minNonZeroElem)
> > ? ? ? ? minElement = find(currRow == minNonZeroElem);
> > ? ? ? ? proc1Matrix(i, minElement) = minNonZeroElem
> > ? ? end
> > end
> > Z=sum(sum(proc1Matrix))
>
> t =[0 1430 0 360 460; 0 0 30 0 0; 0 0 0 90 190; 0 0 0 0 0];
>
> proc1Matrix = zeros(size(t));
> proc2Matrix = zeros(size(t));
> for i = 1:size(t, 1);
> currRow = t(i, :);
> nonZeroElems = nonzeros(currRow);
> minNonZeroElem = min(nonZeroElems);
> if ~isempty(minNonZeroElem)
> minElement = find(currRow == minNonZeroElem);
> if length(find(proc1Matrix(:,mod(i,5)+1))) > 0
> t(i,minElement) = 0;
> currRow = t(i,:);
> nonZeroElems = nonzeros(currRow);
> minNonZeroElem = min(nonZeroElems);
> minElement = find(currRow == minNonZeroElem);
> end
> proc1Matrix(i, minElement) = minNonZeroElem
> end
> end
> Z=sum(sum(proc1Matrix))
>
> Here's one way.
> Just check to see if the column the new minimum is in already has a
> value, if so, go to the next minimum value in that row.

Yes Nathan, this is the solution!!!
I love you!
Thanks!

Subject: Matrix manipulation

From: Milos Milenkovic

Date: 20 Jun, 2009 10:17:01

Message: 4 of 4

There all,
my optimization problem is doing almoust well, but I can't figure out why it is make mastake in just one column. The first part gives a set of feasible solutions. Then in second part I have to extract from the solution matrix t minimum values, so in each column and row to be just one minimum value. I have two minimum values in one column and in another there is no values. If it is already chosen minimum value in some column it is needed to find another value, first higher.
Please help me!
m=11;
n=11;
A=[1 1 370 2 505 30; 2 2 833 1 933 30; 3 1 510 3 695 30; 4 3 1038 1 1224 30; 5 1 220 4 545 30; 6 4 805 5 840 10; 7 5 865 4 915 30; 8 4 1185 6 1253 30;
    9 6 361 4 433 30; 10 4 635 7 852 30; 11 7 880 4 1094 30;12 4 1152 1 71 30]

for i=1:1:m
        j=2;
           for l=1:1:m
              if A(i,j+2)==A(l,j) & l~=i
                    x(i,l)=1;
                  if A(l,j+1)-A(i,j+3)>=A(i,6) & A(l,j+1)-A(i,j+3)>0;
                     t(i,l)=A(l,j+1)-A(i,j+3)-A(i,6);
                     
                  else
                      t(i,l)=1440+(A(l,j+1)-A(i,j+3));
                  end
              else
                 x(i,l)=0;
                 t(i,l)=0;
              end
           end
end

display (x)
display(t)
%Following part have to give an optimal set of solution so in each row and in each column to be minimum value, but with condition to have just one value in each row and each column, there is no column with two values and row with two values. BUt J obtained one column with two values. Why?
proc1Matrix = zeros(size(t));
proc2Matrix = zeros(size(t));
for i = 1:size(t, 1);
    currRow = t(i, :);
    nonZeroElems = nonzeros(currRow);
    minNonZeroElem = min(nonZeroElems);
    if ~isempty(minNonZeroElem)
        minElement = find(currRow == minNonZeroElem);
        if length(find(proc1Matrix(:,mod(i,12)+1))) > 0
           t(i,minElement) = 0;
           currRow = t(i,:);
           nonZeroElems = nonzeros(currRow);
           minNonZeroElem = min(nonZeroElems);
           minElement = find(currRow == minNonZeroElem);
        end
        proc1Matrix(i, minElement) = minNonZeroElem
    end
end
Z=sum(sum(proc1Matrix))

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us