Solving an iterative procedure with respect to some criteria using MATLAB
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I would like to solve the following iterative algorithm based on the given incomplete matrix A, where `-` refers to the missing entries of A (9 missing entries in total):
A = [...
0.5 0.2 0.6 0.4;...
- 0.5 - - ;...
- - 0.5 - ;...
- - - 0.5];
Now I want to fill in the incomplete matrix A by following an iterative algorithm using a function of two variables f(i,k) = i+k+3 and respecting the following criteria here
; ; and ;
**EMV_h** denotes the subset of missing values **MV** that can be estimated in step h; **UV_h** denotes Unknown Values in step h; **KV_h** denotes Known Values in step h; **KnownValue** denotes the nondiagonal nonzero entries of A. Hik_1, Hik_2, and Hik_3 are already written/defined in the Matlab code (it is fine!): i.e.,
; ; and .
For instance, in this matrix:
MV = {[2,1], [3,1], [4,1], [3,2], [4,2], [2,3], [4,3], [2,4],[3,4]}; KnownValue = {[1,2], [1,3], [1,4]};
when h=1, EMV_1 = {[3,2], [4,2], [2,3], [4,3], [2,4],[3,4]}; And when h=2, EMV_2 = {[2,1], [3,1], [4,1]};
The Algorithm is:
Step 0. EMV_0 = empty set;
Step 1. h=1;
Step 2. while EMV_h ~= empty set {
Step 3. for every (i,k) in EMV_h {
Step 4. f(i,k)
Step 5. }
Step 6. h=h++
Step 7. }
I wrote a MATLAB code as follows and it works correctly when h=1. i.e., when EMV_1 = MV. But I couldn't go further when `h=2,3,4,...` i.e. when `**EMV_h ~= MV**`. My question/challenge is to construct `EMV_h` and manage it in the Matlab code. How can I write the Matlab code for EMV_h, please? . Thanks in advance!
clear;clc
global P1;
global n;
% % Incomplete Matrix
% 0 indicates the missing entry position
A = [...
0.5 0.2 0.6 0.4 ;...
0 0.5 0 0 ;...
0 0 0.5 0 ;...
0 0 0 0.5];
n = size(A,1);
M = A; % to write Missing Values (MV)
P1 = A; % to write the nondiagonal Known (Estimated) Values (EV)
P1(1:n+1:end)=0; % To make the diagonal elements zero
%i.e, to make all P zero except the known entries in the nondiagonal position
P1;
% To write Missing Values
[r,s] = find(~M); % Find indices and values of zero elements of P
MV = num2cell([r,s] ,2); %to write the missing values in array
MV;
% The algorithm
EMV = {}; % Estimated missing value, EMV = empty set
h = 1;
while isempty(EMV)
EMV = MV; % Missing Values
index = 1; % index
% for every (i,k) in EMV
for m = 1:length(EMV)
i = EMV{m}(1); % row of EMV
k = EMV{m}(2); % column of EMV
EMV{index} = f(i,k); % puts value in the position of A(i,k)
index = index+1;
end
EMV; % the value of A(i,k) for any given (i,k), cell array form
EMV = cell2mat(EMV); % values in column form
% To write a complete matrix
for s = 1:length(EMV) % s is another index
% put EMV(s) in the position (i,k);
A(MV{s}(1),MV{s}(2)) = EMV(s);
end
A
% EMVh{h} = {EMV}
% h = h+1;
end
function pik = f(i,k)
global P1;
global n;
% to write the nondiagonal Known values (EV)
[u,v]=find(P1); % Find indices and values of nonzero elements of P (nondiagonal)
KnownValue = num2cell( [u,v] ,2); %to write the Known Values in array
%
% Hik_1 = {j~=i,k| (i,j),(j,k) in KnownValue}
Hik_1=setdiff( intersect(v(u==i),u(v==k)) ,[i,k]);
% Hik_2 = {j~=i,k| (j,k),(j,i) in KnownValue}
Hik_2=setdiff( intersect(u(v==i),u(v==k)) ,[i,k]) ;
% Hik_3 = {j~=i,k| (i,j),(k,j) in KnownValue}
Hik_3=setdiff( intersect(v(u==i),v(u==k)) ,[i,k]) ;
% To calculate pik
if ~isempty(Hik_1)||~isempty(Hik_2)||~isempty(Hik_3)
pik = i+k+3;
else
pik =0;
end
pik
end
The answer I got: when h=1,
A = [...
0.5000 0.2000 0.6000 0.4000;...
0 0.5000 8.0000 9.0000;...
0 8.0000 0.5000 10.0000;...
0 9.0000 10.0000 0.5000];
When h=2, the answer should be the following, but I couldn't manage to run it inside the while loop at once. I obtained this result after I run it two times independently.
A = [...
0.5000 0.2000 0.6000 0.4000
6.0000 0.5000 8.0000 9.0000
7.0000 8.0000 0.5000 10.0000
8.0000 9.0000 10.0000 0.5000];
Risposta accettata
Karthik P.V
il 15 Dic 2021
Modificato: Karthik P.V
il 15 Dic 2021
Please try this and confirm
clear;clc
global P1;
global n;
% % Incomplete Matrix
% 0 indicates the missing entry position
A = [...
0.5 0.2 0.6 0.4 ;...
0 0.5 0 0 ;...
0 0 0.5 0 ;...
0 0 0 0.5];
for j=1:4%Enter count of h <New>
n = size(A,1);
M = A; % to write Missing Values (MV)
P1 = A; % to write the nondiagonal Known (Estimated) Values (EV)
P1(1:n+1:end)=0; % To make the diagonal elements zero
%i.e, to make all P zero except the known entries in the nondiagonal position
P1;
% To write Missing Values
[r,s] = find(~M); % Find indices and values of zero elements of P
MV = num2cell([r,s] ,2); %to write the missing values in array
MV;
% The algorithm
EMV = {}; % Estimated missing value, EMV = empty set
h = j; % <New>
if isempty(EMV) && ~isempty(MV) % <Mod>
EMV = MV; % Missing Values
index = 1; % index
% for every (i,k) in EMV
for m = 1:length(EMV)
i = EMV{m}(1); % row of EMV
k = EMV{m}(2); % column of EMV
EMV{index} = f(i,k); % puts value in the position of A(i,k)
index = index+1;
end
EMV; % the value of A(i,k) for any given (i,k), cell array form
EMV = cell2mat(EMV); % values in column form
% To write a complete matrix
for s = 1:length(EMV) % s is another index
% put EMV(s) in the position (i,k);
A(MV{s}(1),MV{s}(2)) = EMV(s);
end
A
% EMVh{h} = {EMV}
% h = h+1;
end
% EMV = {};
end
function pik = f(i,k)
global P1;
global n;
% to write the nondiagonal Known values (EV)
[u,v]=find(P1); % Find indices and values of nonzero elements of P (nondiagonal)
KnownValue = num2cell( [u,v] ,2); %to write the Known Values in array
%
% Hik_1 = {j~=i,k| (i,j),(j,k) in KnownValue}
Hik_1=setdiff( intersect(v(u==i),u(v==k)) ,[i,k]);
% Hik_2 = {j~=i,k| (j,k),(j,i) in KnownValue}
Hik_2=setdiff( intersect(u(v==i),u(v==k)) ,[i,k]) ;
% Hik_3 = {j~=i,k| (i,j),(k,j) in KnownValue}
Hik_3=setdiff( intersect(v(u==i),v(u==k)) ,[i,k]) ;
% To calculate pik
if ~isempty(Hik_1)||~isempty(Hik_2)||~isempty(Hik_3)
pik = i+k+3;
else
pik =0;
end
pik
end
3 Commenti
Karthik P.V
il 16 Dic 2021
clear;clc
global P1;
global n;
% % Incomplete Matrix
% 0 indicates the missing entry position
A = [...
0.5 0.2 0.6 0.4 ;...
0 0.5 0 0 ;...
0 0 0.5 0 ;...
0 0 0 0.5];
h=0;
EMV=[];
MV=0;
while (isempty(EMV) && ~isempty(MV))%for j=1:2%Enter count of h <New>
n = size(A,1);
M = A; % to write Missing Values (MV)
P1 = A; % to write the nondiagonal Known (Estimated) Values (EV)
P1(1:n+1:end)=0; % To make the diagonal elements zero
%i.e, to make all P zero except the known entries in the nondiagonal position
P1;
% To write Missing Values
[r,s] = find(~M); % Find indices and values of zero elements of P
MV = num2cell([r,s] ,2); %to write the missing values in array
MV;
% The algorithm
EMV = {}; % Estimated missing value, EMV = empty set
% h = j; % <New>
while (isempty(EMV) && ~isempty(MV)) % <Mod>
EMV = MV; % Missing Values
index = 1; % index
% for every (i,k) in EMV
for m = 1:length(EMV)
i = EMV{m}(1); % row of EMV
k = EMV{m}(2); % column of EMV
EMV{index} = f(i,k); % puts value in the position of A(i,k)
index = index+1;
end
EMV; % the value of A(i,k) for any given (i,k), cell array form
EMV = cell2mat(EMV); % values in column form
% To write a complete matrix
for s = 1:length(EMV) % s is another index
% put EMV(s) in the position (i,k);
A(MV{s}(1),MV{s}(2)) = EMV(s);
end
A
% EMVh{h} = {EMV}
h = h+1;
end
EMV = {};
end
function pik = f(i,k)
global P1;
global n;
% to write the nondiagonal Known values (EV)
[u,v]=find(P1); % Find indices and values of nonzero elements of P (nondiagonal)
KnownValue = num2cell( [u,v] ,2); %to write the Known Values in array
%
% Hik_1 = {j~=i,k| (i,j),(j,k) in KnownValue}
Hik_1=setdiff( intersect(v(u==i),u(v==k)) ,[i,k]);
% Hik_2 = {j~=i,k| (j,k),(j,i) in KnownValue}
Hik_2=setdiff( intersect(u(v==i),u(v==k)) ,[i,k]) ;
% Hik_3 = {j~=i,k| (i,j),(k,j) in KnownValue}
Hik_3=setdiff( intersect(v(u==i),v(u==k)) ,[i,k]) ;
% To calculate pik
if ~isempty(Hik_1)||~isempty(Hik_2)||~isempty(Hik_3)
pik = i+k+3;
else
pik =0;
end
pik
end
Can you try this?
Note: I have not captured the algorithm changes here
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!