how to select values from matrix?

1 次查看(过去 30 天)
Mitali Oraw
Mitali Oraw 2013-10-25
回答: Prateekshya 2024-10-15
I have 10 cores C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,, each core have their own testing time,, I represent the testing time of each core by matrix A where each row corresponds to core no.
A = [ 428,220,155,116,103,90,77,64,64,63,51,51,51,51,50,38;
15292,7719,4067,3175,2655,2070,1774,1624,1405,1257,1182,1107,1034,960,886,813;
5058,2582,2507,2507,2507,2507,2507,2507,2507,2507,2507,2507,2507,2507,2507,2507;
26602,13354,11129,6782,5829,5829,5829,5829,5829,5829,5829,5829,5829,5829,5829,5829;
191874,95992,64070,48106,38481,32167,27613,24163,21518,19757,17624,16194,14983,14873,14873,12192;
185794,93014,62248,46741,37364,31241,28199,23488,20906,19034,19034,18799,18799,18564,18564,11978;
65686,32891,21959,16493,13243,11027,9695,8342,7383,6717,6431,6431,6431,6431,6431,4219;
22427,11262,8721,5680,4605,4605,4605,4605,4605,4605,4605,4605,4605,4605,4605,4605;
26351,13182,8802,6597,5310,4440,3798,3305,2964,2820,2418,2226,2118,2118,2118,1659;
120188,60128,40137,30132,24701,21182,17663,15100,14144,14144,11033,10625,10625,10625,10625,7586 ] ;
And also I have represent all the core by matrix Z
Z=[6,10,5,4;7,8,3,0;1,2,9,0];
each element in matrix Z correspond to the no of rows of matrix A. I have generate a temporary matrix temp which always produce random number and summation of each element in a must not be greater than 16. the code is
highest_num=14;
n=randperm(14);
c=combnk(n,3);
t=1;
for i=1:((highest_num*(highest_num-1)*(highest_num-2))/(3*2*1))
sum=0;
for j=1:3
sum=sum+c(i,j);
end
if(sum==16)
m=1;
for k=1:3
if(m<4)
temp(t,k)=c(i,m);
m=m+1;
end
end
t=t+1;
end
end
suppose temp matrix is
temp =[ 9 6 1 ; 9 2 5 ; 9 4 3 ; 6 2 8 ; 6 7 3 ; 1 2 13 ; 1 8 7 ; 1 12 3 ;1 5 10 ; 1 4 11 ; 2 4 10 ; 2 3 11 ; 8 5 3 ; 5 4 7]
I have to pick values from matrix A according to mapping between matrix Z and matrix temp,, I want to say that the 1st element of 1st row and 1st column of temp is 9 and the 1st element of 1st row and 1st column of Z is 6 so i have to pick the value of row no 6 and colum no 9 from matrix A afterthat the element of 2nd row and 1st column of Z is 7 so i have pick value of row no 7 and col no 9 from matrix A, afterthat the element of 3rd row and 1st column of Z is 1 so i have pick value of row no 1 and col no 9 from matrix A,, then i have to do summation of of these trhee values,,likewise I have to pick values from A for 2nd column and 3rd column of Z and temp and calculate the summation of 2nd column and 3rd column.. the total_testingTime is the maximum among three summation.
Now i have apply simulated annealing for selecting the best total_testingTime so the above technique has to be done until temperature>1 the initial temperature = 100 and the cooling_rate = 5
so can u please me suggest me to perform this code in matlab

回答(1 个)

Prateekshya
Prateekshya 2024-10-15
Hello Mitali,
To implement the described process in MATLAB, you will need to follow these steps:
  • Initialize Parameters: Define your initial temperature, cooling rate, and any other necessary parameters.
  • Simulated Annealing Loop: Use a while loop to perform the simulated annealing process until the temperature drops below a threshold.
  • Generate Random Permutations: Use the randperm and combnk functions to generate combinations. For more information on combnk and nchoosek, please go through: https://www.mathworks.com/help/stats/combnk.html. For randperm please follow: https://www.mathworks.com/help/matlab/ref/randperm.html.
  • Mapping and Summation: For each valid permutation, map the values from temp to Z and then to A to calculate the total testing time.
  • Update Best Solution: Check if the current solution is better than the best found so far and update accordingly.
  • Cooling Schedule: Reduce the temperature using the cooling rate.
Here is a sample MATLAB code to get you started:
% Initialize parameters
initial_temperature = 100;
cooling_rate = 5;
temperature = initial_temperature;
highest_num = 14;
% Define matrices A and Z
A = [...]; % Your matrix A
Z = [6, 10, 5, 4; 7, 8, 3, 0; 1, 2, 9, 0];
% Initialize best total testing time
best_total_testingTime = Inf;
% Simulated annealing loop
while temperature > 1
% Generate random permutations
n = randperm(highest_num);
c = combnk(n, 3);
% Initialize temporary matrix
temp = [];
t = 1;
% Select combinations that sum to 16
for i = 1:size(c, 1)
if sum(c(i, :)) == 16
temp(t, :) = c(i, :);
t = t + 1;
end
end
% Iterate over each column of Z
for col = 1:size(Z, 2)
if Z(1, col) == 0, continue; end % Skip if Z element is zero
% Calculate total testing time for this column
total_testingTime = 0;
for row = 1:size(temp, 1)
for z_row = 1:size(Z, 1)
if Z(z_row, col) == 0, continue; end % Skip if Z element is zero
% Map temp to Z and then to A
core_number = Z(z_row, col);
temp_value = temp(row, z_row);
total_testingTime = total_testingTime + A(core_number, temp_value);
end
end
% Update best total testing time
if total_testingTime < best_total_testingTime
best_total_testingTime = total_testingTime;
end
end
% Cool down
temperature = temperature - cooling_rate;
end
% Display the best total testing time found
disp(['Best Total Testing Time: ', num2str(best_total_testingTime)]);
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Simulated Annealing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by