Implementation of a deep search to solve games with Matlab - matlab

I have this code but it is implemented with a breadth search algorithm, as I could implement it being a depth search algorithm.
What I want to achieve is that having an initial matrix of 3x3 [4, 1, 2; 7, 5, 3; 0, 8, 6] is to get to the next array [1, 2, 3; 4, 5, 6; 7, 8, 0]
Using depth first search since the code below is wide.
%matlab
I= [4, 1, 2; 7, 5, 3; 0, 8, 6]
G=
[1, 2, 3; 4, 5, 6; 7, 8, 0]
F={};
Fparent = {};
P=I;
i=1;
S(i,:,:) = P(:,:);
Sparent(i) = 0;
while not(isequal(P,G))
[x,y] = find(P==0);
if(x > 1)
C = P;
C(x,y) = P(x-1,y);
C(x-1,y) = 0;
F{end+1} = C;
Fparent{end+1} = i;
end
if(x < 3)
C = P;
C(x,y) = P(x+1,y);
C(x+1,y) = 0;
F{end+1} = C;
Fparent{end+1} = i;
end
if(y > 1)
C = P;
C(x,y) = P(x,y-1);
C(x,y-1) = 0;
F{end+1} = C;
Fparent{end+1} = i;
end
if(y < 3)
C = P;
C(x,y) = P(x,y+1);
C(x,y+1) = 0;
F{end+1} = C;
Fparent{end+1} = i;
end
P=F{1}; %BFS
i = i+1;
S(i,:,:) = P(:,:);
Sparent(i) = Fparent{1};
F = F(2:end);
Fparent = Fparent(2:end);
end
P(:,:) = S(i,:,:);
idp = Sparent(i);
path = {P};
while not(isequal(P,I))
idp = Sparent(idp);
P(:,:) = S(idp,:,:);
path{end+1} = P;
end
path = flip(path);
path{:}

Related

Using setPostSimFcn to process the outputs of a simulation each time

My model 'notch' is a microgrid model.
and I have three phase currents [i1,i2, i3] measured and sent to the workspace.
It is to produce a signal signature for the three currents which is [x].
then store the signal signature[ x ]in a matrix .
I made a function 'F' which takes the three currents as inputs and produces x as output.
But as I run the code down the three rows in [table] are the same.
how can i use setpostsimfcn and store the result in a matrix and save it?
mdl = 'notch';
find_system(mdl);
mass=3:-1:1
m = length(mass);
for i = m:-1:1
a=4-i;
simin(i) = Simulink.SimulationInput(mdl);
simin(i)=simin(i).setBlockParameter('notch/Lin1',
'Length',num2str(mass(i)));
out(i) = sim(simin(i));
fi=#F; %calling function F
y = fi( i1,i2,i3);
table(i,:)=y;
end
function x = F(i1,i2,i3)
currentA = i1;
currentB = i2;
currentC = i3;
wv = 'db4';
Fs = 3200;
fb = dwtfilterbank
('Wavelet',wv,'Level',4,'SamplingFrequency',Fs);
[cA1, LA1] = wavedec(currentA, 1, 'db4');
[cB1, LB1] = wavedec(currentB, 1, 'db4');
[cC1, LC1] = wavedec(currentC, 1, 'db4');
[cA2, LA2] = wavedec(currentA, 2, 'db4');
[cB2, LB2] = wavedec(currentB, 2, 'db4');
[cC2, LC2] = wavedec(currentC, 2, 'db4');
[cA3, LA3] = wavedec(currentA, 3, 'db4');
[cB3, LB3] = wavedec(currentB, 3, 'db4');
[cC3, LC3] = wavedec(currentC, 3, 'db4');
[cA4, LA4] = wavedec(currentA, 4, 'db4');
[cB4, LB4] = wavedec(currentB, 4, 'db4');
[cC4, LC4] = wavedec(currentC, 4, 'db4');
dA1 = detcoef(cA1, LA1, 1);
dB1 = detcoef(cB1, LB1, 1);
dC1 = detcoef(cC1, LC1, 1);
dA2 = detcoef(cA2, LA2,2);
dB2 = detcoef(cB2, LB2, 2);
dC2 = detcoef(cC2, LC2, 2);
dA3 = detcoef(cA3, LA3, 3);
dB3 = detcoef(cB3, LB3, 3);
dC3 = detcoef(cC3, LC3, 3);
dA4 = detcoef (cA4, LA4, 4);
dB4 = detcoef(cB4, LB4, 4);
dC4 = detcoef(cC4, LC4, 4);
aA4 = appcoef(cA4, LA4,'db4', 4);
aB4 = appcoef(cB4, LB4,'db4', 4);
aC4 = appcoef(cC4, LC4,'db4' ,4);
aA1 = appcoef(cA1, LA1,'db4', 1);
aB1 = appcoef(cB1, LB1,'db4', 1);
aC1 = appcoef(cC1, LC1,'db4' ,1);
x=[aA4 ;dA1 ; dA2 ; dA3 ; dA4 ; aB4; dB1 ; dB2 ;
dB3 ; dB4; aC4; dC1 ; dC2 ; dC3 ; dC4];
end ```

Selecting elements from a large matrix using a sliding window

I have a 220*366 matrix, I want to set a 3*3 windows to select elements in matrix.
example:
matrix = [1, 2, 3, 4, 5;
6, 7, 8, 9, 10;
11,12,13,14,15];
window = [1, 2, 3;
6, 7, 8;
11,12,13];
If you have the Image Processing Toolbox, this can be done using nlfilter:
function out = q52158450()
% Create a matrix:
Rm = 220; Cm = 366;
matrix = randn(Rm,Cm);
% Define the window and apply filter:
Rw = 6; Rc = 6;
windows = nlfilter(matrix, [Rw, Rc], #getwindow);
% Select subset of outputs, removing zero-padded boundary elements:
Bw = floor((Rw-1)/2); Bc = floor((Rc-1)/2);
out = windows(Bw:end-Bw, Bc:end-Bc); % < Output is a cell
function out = getwindow(in)
out = {in};
Otherwise it can be done with a double loop:
function out = q52158450()
% Create a matrix:
Rm = 220; Cm = 366;
matrix = randn(Rm,Cm);
% Define the window and apply filter:
Wr = 3; Wc = 3;
Br = ceil(Wr/2); Bc = ceil(Wc/2);
out = cell(Rm-Br, Cm-Bc);
for indR = 1:Rm-Wr+1
for indC = 1:Cm-Wc+1
out{indR, indC} = matrix(indR:indR + Wr - 1, indC:indC + Wc - 1);
end
end

Insert vector into vector, Matlab

This code is to cut random values from the vector ch and create new vector a. Then, insert a into ch after delete a selected values from ch
What I should change, to be the result like this:
for example if a = [8; 4; 9], then the result :
ch = 5 8 4 9 6 7
Matlab code:
ch = [4; 5; 6; 7; 8; 9];
for i = 1:3
g = randi(3);
a(i) = ch(g);
ch(g) = [];
end;
startIdx = 2;
finalIdx = startIdx + size(a,1) - 1;
ch(startIdx:finalIdx) = a;
disp (ch);
Try this:
ch = [4; 5; 6; 7; 8; 9];
for i = 1:3
g = randi(3);
a(i) = ch(g);
ch(g) = [];
end;
a = a'; % your problem probably come from mixing column / lines
startIdx = 2;
ch = [ch(1:startIdx); a; ch(startIdx+1:end)];
disp (ch);

how can I fix this error code in cbir using wavelet transform and color histogram?

I have a project of CBIR using wavelet transform and color histogram. I perform re-experiment from Singha and Hemachandran research. I have tried to code it, but when image was added in database, it has been error. The error message states :
Error using database/fastinsert (line 222)
Cell array or struct should not contain vectors
Error in koneksidbEkstraksi (line 11)
fastinsert(conn, 'ekstraksifitur', colciri, rowciri);
Error in metode_ekstraksi (line 157)
koneksidbEkstraksi(A, H, V);
Error in tambah>btn_tmbhcitra_Callback (line 361)
metode_ekstraksi(input);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in tambah (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)tambah('btn_tmbhcitra_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I think it was caused by algorithm of extraction code. Here is the code.
function metode_ekstraksi(citra)
%extract component of RGB
r = citra(:,:,1);
g = citra(:,:,2);
b = citra(:,:,3);
%decompose each Red, Green, Blue component
[R,SR] = wavedec2(r,1,'db2');
LL1R = appcoef2(R,SR,1,'db2');
[HL1R,LH1R,HH1R] = detcoef2('all',R,SR,1);
[G,SG] = wavedec2(g,1,'db2');
LL1G = appcoef2(G,SG,1,'db2');
[HL1G,LH1G,HH1G] = detcoef2('all',G,SG,1);
[B,SB] = wavedec2(b,1,'db2');
LL1B = appcoef2(B,SB,1,'db2');
[HL1B,LH1B,HH1B] = detcoef2('all',B,SB,1);
%combine approximate, horizontal, vertical coefficient of RGB component
A1(:,:,1) = LL1R;
A1(:,:,2) = LL1G;
A1(:,:,3) = LL1B;
H1(:,:,1) = HL1R;
H1(:,:,2) = HL1G;
H1(:,:,3) = HL1B;
V1(:,:,1) = LH1R;
V1(:,:,2) = LH1G;
V1(:,:,3) = LH1B;
%assign the weights
BA = 0.003*A1;
BH = 0.001*H1;
BV = 0.001*V1;
[rowsA, colsA, numOfBands] = size(BA);
[rowsH, colsH, numOfBands] = size(BH);
[rowsV, colsV, numOfBands] = size(BV);
%convert coefficient into hsv plane
hsvA = rgb2hsv(BA);
ha = hsvA(:, :, 1);
sa = hsvA(:, :, 2);
va = hsvA(:, :, 3);
hsvH = rgb2hsv(BH);
hh = hsvH(:, :, 1);
sh = hsvH(:, :, 2);
vh = hsvH(:, :, 3);
hsvV = rgb2hsv(BV);
hv = hsvV(:, :, 1);
sv = hsvV(:, :, 2);
vv = hsvV(:, :, 3);
% Specify the number of quantization levels.
numberOfLevelsForH = 8;
numberOfLevelsForS = 8;
numberOfLevelsForV = 8;
% Find the max.
maxValueForHa = max(ha(:));
maxValueForSa = max(sa(:));
maxValueForVa = max(va(:));
maxValueForHh = max(hh(:));
maxValueForSh = max(sh(:));
maxValueForVh = max(vh(:));
maxValueForHv = max(hv(:));
maxValueForSv = max(sv(:));
maxValueForVv = max(vv(:));
% create final histogram matrix of size 8x2x2
A = zeros(8, 8, 8);
H = zeros(8, 8, 8);
V = zeros(8, 8, 8);
% create col vector of indexes for later reference
indexA = zeros(rowsA*colsA, 3);
indexH = zeros(rowsH*colsH, 3);
indexV = zeros(rowsV*colsV, 3);
% Put all pixels into one of the "numberOfLevels" levels.
count = 1;
for rowA = 1:size(ha, 1)
for colA = 1 : size(ha, 2)
quantizedValueForHa(rowA, colA) = ceil(numberOfLevelsForH * ha(rowA, colA)/maxValueForHa);
quantizedValueForSa(rowA, colA) = ceil(numberOfLevelsForS * sa(rowA, colA)/maxValueForSa);
quantizedValueForVa(rowA, colA) = ceil(numberOfLevelsForV * va(rowA, colA)/maxValueForVa);
% keep indexes where 1 should be put in matrix hsvHist
indexA(count, 1) = quantizedValueForHa(rowA, colA);
indexA(count, 2) = quantizedValueForSa(rowA, colA);
indexA(count, 3) = quantizedValueForVa(rowA, colA);
countA = count+1;
end
end
for rowH = 1:size(hh, 1)
for colH = 1 : size(hh, 2)
quantizedValueForHh(rowH, colH) = ceil(numberOfLevelsForH * hh(rowH, colH)/maxValueForHh);
quantizedValueForSh(rowH, colH) = ceil(numberOfLevelsForS * sh(rowH, colH)/maxValueForSh);
quantizedValueForVh(rowH, colH) = ceil(numberOfLevelsForV * vh(rowH, colH)/maxValueForVh);
% keep indexes where 1 should be put in matrix hsvHist
indexH(count, 1) = quantizedValueForHh(rowH, colH);
indexH(count, 2) = quantizedValueForSh(rowH, colH);
indexH(count, 3) = quantizedValueForVh(rowH, colH);
countH = count+1;
end
end
for rowV = 1:size(hv, 1)
for colV = 1 : size(hv, 2)
quantizedValueForHv(rowV, colV) = ceil(numberOfLevelsForH * hv(rowV, colV)/maxValueForHv);
quantizedValueForSv(rowV, colV) = ceil(numberOfLevelsForS * sv(rowV, colV)/maxValueForSv);
quantizedValueForVv(rowV, colV) = ceil(numberOfLevelsForV * vv(rowV, colV)/maxValueForVv);
% keep indexes where 1 should be put in matrix hsvHist
indexV(count, 1) = quantizedValueForHv(rowV, colV);
indexV(count, 2) = quantizedValueForSv(rowV, colV);
indexV(count, 3) = quantizedValueForVv(rowV, colV);
countV = count+1;
end
end
% put each value of h,s,v to matrix 8x2x2
% (e.g. if h=7,s=2,v=1 then put 1 to matrix 8x2x2 in position 7,2,1)
for rowA = 1:size(indexA, 1)
if (indexA(rowA, 1) == 0 || indexA(rowA, 2) == 0 || indexA(rowA, 3) == 0)
continue;
end
A(indexA(rowA, 1), indexA(rowA, 2), indexA(rowA, 3)) = ...
A(indexA(rowA, 1), indexA(rowA, 2), indexA(rowA, 3)) + 1;
end
for rowH = 1:size(indexH, 1)
if (indexH(rowH, 1) == 0 || indexH(rowH, 2) == 0 || indexH(rowH, 3) == 0)
continue;
end
H(indexH(rowH, 1), indexH(rowH, 2), indexH(rowH, 3)) = ...
H(indexH(rowH, 1), indexH(rowH, 2), indexH(rowH, 3)) + 1;
end
for rowV = 1:size(indexV, 1)
if (indexV(rowV, 1) == 0 || indexV(rowV, 2) == 0 || indexV(rowV, 3) == 0)
continue;
end
V(indexV(rowV, 1), indexV(rowV, 2), indexV(rowV, 3)) = ...
V(indexV(rowV, 1), indexV(rowV, 2), indexV(rowV, 3)) + 1;
end
% normalize hsvHist to unit sum
A = A(:)';
A = A/sum(A);
H = H(:)';
H = H/sum(H);
V = V(:)';
V = V/sum(V);
koneksidbEkstraksi(A, H, V);
Before that, I also show code of database connection for result of extraction. Here is the code.
function koneksidbEkstraksi(App, Hor, Ver)
conn=database('cbir018','root','');
Cciri = exec(conn, 'select max(kd_ekstraksi)+1 from ekstraksifitur');
Cciri = fetch(Cciri);
%Cciri.Data
kd=Cciri.Data{1,1};
colciri = {'kd_ekstraksi' 'koe_app' 'koe_hor' 'koe_ver'};
rowciri = {kd, App, Hor, Ver};
fastinsert(conn, 'ekstraksifitur', colciri, rowciri);
close(conn);
I hope anyone can help me to solve this problem. Thank you.

Linearization of 5D Matrix in Matlab

Say I have Matrix A, defined in MATLAB as:
A = zeros(2,2,2,2,2);
A(1,1,1,1,1) = 1;
A(1,1,1,1,2) = 2;
A(1,1,1,2,1) = 3;
A(1,1,1,2,2) = 4;
A(1,1,2,1,1) = 5;
A(1,1,2,1,2) = 6;
A(1,1,2,2,1) = 7;
A(1,1,2,2,2) = 8;
A(1,2,1,1,1) = 9;
A(1,2,1,1,2) = 10;
A(1,2,1,2,1) = 11;
A(1,2,1,2,2) = 12;
A(1,2,2,1,1) = 13;
A(1,2,2,1,2) = 14;
A(1,2,2,2,1) = 15;
A(1,2,2,2,2) = 16;
A(2,1,1,1,1) = 17;
A(2,1,1,1,2) = 18;
A(2,1,1,2,1) = 19;
A(2,1,1,2,2) = 20;
A(2,1,2,1,1) = 21;
A(2,1,2,1,2) = 22;
A(2,1,2,2,1) = 23;
A(2,1,2,2,2) = 24;
A(2,2,1,1,1) = 25;
A(2,2,1,1,2) = 26;
A(2,2,1,2,1) = 27;
A(2,2,1,2,2) = 28;
A(2,2,2,1,1) = 29;
A(2,2,2,1,2) = 30;
A(2,2,2,2,1) = 31;
A(2,2,2,2,2) = 32;
How can I convert this Matrix into a new Matrix B, such that matrix B is a vector such that B = [1,2,3,4...32]?
The reshape function isn't working and I can't find anything online to help.
Thanks in advance!
You have the dimensions in the opposite order to achieve what you want. So: just permute the dimensions (with permute) and then linearize (with :):
B = permute(A, [5 4 3 2 1]);
B = B(:);
If you want it in one line, use reshape for the linearization:
B = reshape(permute(A, [5 4 3 2 1]), [],1);
If you defined (note the reversal of indices)
A(1,1,1,1,1) = 1;
A(2,1,1,1,1) = 2;
A(1,2,1,1,1) = 3;
A(2,2,1,1,1) = 4;
A(1,1,2,1,1) = 5;
A(2,1,2,1,1) = 6;
A(1,2,2,1,1) = 7;
A(2,2,2,1,1) = 8;
...
the dimensions would be in the "right" order, and then you wouldn't need permute:
B = A(:);
or
B = reshape(A, [],1);