clc, clear all % The code is Matlab code % Any one be so kind and help me, Thank
ID: 642333 • Letter: C
Question
clc, clear all
% The code is Matlab code
% Any one be so kind and help me, Thank alot !
% I have a 4x4 matrix X and I want to optimize and find the minimum
% element of the matrix so that the sum of all the min will be the
%optimum = 207.
% The min only alow to occur once, either in a column or in a row.
% In other words, the min are strictly not alow to be in the same row or
% column the optimum will be 207, but i run this code i got
% optimum = 205, which is not correct.
X=[54 54 51 53; 51 57 52 52; 50 53 54 56; 56 54 55 53];
for i=1:length(X)
for j=1:length(X)
A(i,:)=min(X(i,:));
Optimum=sum(A);
end
end
A
Optimum
Explanation / Answer
X=[54 54 51 53; 51 57 52 52; 50 53 54 56; 56 54 55 53];
[row column]=size(X);
board = zeros(1,row);
chosen = zeros(1,row);
optimum = sum(sum(X));
for i = 1:row
board(1)=i;
[board optimum] = isSolution(X,board,2,optimum);
end
optimum
%% isSolution.m %%
function [board optimum] = isSolution(X,board,depth,optimum)
[row column]=size(X);
if depth == 5
for i = 1:column
r=board(i);
chosen(i)=X(r,i);
end
if sum(chosen) < optimum
optimum=sum(chosen);
end
return
end
for i = 1:row
if isSafe(board,depth,i)
board(depth)=i;
[board optimum] = isSolution(X,board,depth+1,optimum);
end
end
end
%% isSafe.m %%
function value = isSafe(board,depth,counter)
for i = 1:depth-1
if board(i) == counter
value=false;
return;
end
end
value=true;
return;
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.