how to select wanted ( needed situations) dual elements which are randomly generated by using random sampling in matlab? - matlab

I have 2 different random selected variables. The first one is number of bedrooms which is r1 and the second one is number of people in the dwelling which is r2. there are different certain constant values, which are going to be used for selection of house size.Wanted combinations
There are 24 different combinations that both of random generators can produce by using number of bedrooms and number of people in the house. there is no problem if the random generators produce wanted combinations.If not, there is a problem come out which is unwanted combinations.
How can get rid of this unwanted combinations or how to solve this problem in another way?
My code is as follows:
R1 = randsample('xyzq',1,true,[0.1 0.2 0.43 0.27]); % Probability of number of bedrooms in dwellings
r1=R1;
R2 = randsample('abcdef',1,true,[0.283 0.358 0.163 0.134 0.044 0.018]); %Probability of Household size in UK
r2=R2;
if (r1 == 'x' && r2 == 'a') % 37m2 1 bed, 1 per
A_roof = 37;
A_floor = 37;
A_wall = 35;
A_door = 3;
A_windows = 3;
elseif (r1 == 'x' && r2 == 'b') % 50m2 1 bed, 2 per
A_roof = 50;
A_floor = 50;
A_wall = 47;
A_door = 6;
A_windows = 6;
elseif (r1 == 'y' && r2 == 'c') % 61m2 2 bed, 3 per
A_roof = 61;
A_floor = 61;
A_wall = 42;
A_door = 5;
A_windows = 5;
elseif (r1 == 'y' && r2 == 'd') % 70m2 2 bed, 4 per
A_roof = 70;
A_floor = 70;
A_wall = 50;
A_door = 5;
A_windows = 5;
elseif (r1 == 'y' && r2 == 'd') % 74m2 3 bed, 4 per
A_roof = 74;
A_floor = 74;
A_wall = 51;
A_door = 6;
A_windows = 6;
elseif (r1 == 'z' && r2 == 'e') % 86m2 3 bed, 5 per
A_roof = 86;
A_floor = 86;
A_wall = 55;
A_door = 6;
A_windows = 6;
elseif (r1 == 'z' && r2 == 'f') % 95m2 3 bed, 6 per
A_roof = 95;
A_floor = 95;
A_wall = 70;
A_door = 7;
A_windows = 7;
elseif (r1 == 'q' && r2 == 'e') % 90m2 4 bed, 5 per
A_roof = 90;
A_floor = 90;
A_wall = 68;
A_door = 7;
A_windows = 7;
elseif (r1 == 'q' && r2 == 'f') % 99m2 4 bed, 6 per
A_roof = 99;
A_floor = 99;
A_wall = 74;
A_door = 8;
A_windows = 8;
elseif (r1 == 'y' && r2 == 'd') % 83m2 2 bed, 4 per
A_roof = 40;
A_floor = 83;
A_wall = 105;
A_door = 8;
A_windows = 8;
elseif (r1 == 'z') && (r2 == 'd') % 87m2 3 bed, 4 per
A_roof = 42;
A_floor = 87;
A_wall = 105;
A_door = 8;
A_windows = 8;
elseif (r1 == 'z' && r2 == 'e') % 96m2 3 bed, 5 per
A_roof = 46;
A_floor = 96;
A_wall = 150;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'e') % 100m2 4 bed, 5 per
A_roof = 50;
A_floor = 100;
A_wall = 180;
A_door = 10;
A_windows = 10;
elseif(r1 == 'q' && r2 == 'f') % 107m2 4 bed, 6 per
A_roof = 55;
A_floor = 107;
A_wall = 125;
A_door = 10;
A_windows = 10;
elseif (r1 == 'z' && r2 == 'e') % 102m2 3 bed, 5 per
A_roof = 50;
A_floor = 102;
A_wall = 200;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'e') % 106m2 4 bed, 5 per
A_roof = 55;
A_floor = 106;
A_wall = 200;
A_door = 10;
A_windows = 10;
elseif (r1 == 'q' && r2 == 'f') % 113m2 4 bed, 6 per
A_roof = 60;
A_floor = 113;
A_wall = 200;
A_door = 10;
A_windows = 10;
end

Related

Turn vector into matrix when a fixed value is reached

This question is an addition to this one: Link to original question
How can I implement in the code, that all rows should be "filled up" with 0 let's say up to column 6.
This is an example of how it should work.
V [18x1]: [6000, 6500, 5000, 8000, 15000, 15500, 16000, 6000, 4000, 16500, 14000, 400, 5000, 6000, 9000, 12000, 13000, 5000]
Matrix [3x4]:
1.row [8000 15000 15500 16000 0 0]
2.row [16500 14000 0 0 0 0]
3.row [9000 12000 13000 0 0 0]
You should modify the answer likes the following:
result = []; new_row = 1; col_num = 1; row_num = 0;
limit = 7000;
for idx = 1:length(V)
if col_num == 7
new_row = 1
end
if(V(idx) > limit && new_row == 0) % case 1
result(row_num, col_num) = V(idx);
col_num = col_num + 1;
elseif(V(idx) > limit && new_row == 1) %case 2
row_num = row_num + 1; new_row = 0; col_num = 2;
result(row_num, 1) = V(idx);
elseif(V(idx) <= limit) %case 3
new_row = 1;
end
end
if size(result,2) < 6
result(1,6) = 0;
end
Add the follwoing lines to check the col_num does not exceed from 6:
if col_num == 7
new_row = 1
end
At the end, check if the column size of the result is not 6, modify the result matrix likes the following:
if size(result,2) < 6
result(1,6) = 0;
end
Here are the inputs:
% minimum number of columns in the result matrix
min_cols = 6;
% the output from the accepted answer to your original question
result = [ ...
8000 15000 15500 16000 ;
16500 14000 0 0 ;
9000 12000 13000 0];
And here is what you can do to add zeros (append this code to the accepted answer to your original question):
% number of columns to add (if min_cols < number of columns in result,
% will not add any extra columns)
cols_to_add = max(0, min_cols - size(result, 2));
% pad with zeros
result = [result, zeros(size(result, 1), cols_to_add)];

Remove table rows based on condition in matlab

a=[1; 2 ; 3]; b=[ 4; 5; 6 ]; T=table(a,b).
I want to remove rows of table for which the value of b is less than or equal to 5 (b<=5).
You can use logical indexing:
a=[1; 2 ; 3];
b=[ 4; 5; 6 ];
T=table(a,b);
rowidx = (T.b <= 5);
T = T(~rowidx, :);
Which returns:
T =
1×2 table
a b
_ _
3 6
Fast, simple. elegant:
T(T.b <= 5,:) = [];
Another approach:
a = [1; 2; 3];
b = [4; 5; 6];
X = [a, b];
n = 1; m = 1;
while (n <= size(X, 1))
if(X(n, 2) > 5)
X_new(m, :) = X(n, :);
m = m + 1;
end
n = n + 1;
end
'X_new' will be the required matrix.

Dynamic window forming in efficient way in MATLAB

Can someone help me to provide an efficient way or help me to perform the provide code to do make same results in minimal possible steps. I shall be grateful to you.
I have an Original Array:
A = [1 1 1 4.3 4.5 4 4.3 3 1 0 0 2 6.2 6.3 6 6.2 7.4 8 7.2 2 2 3 3 2];
Output Looks like:
A = [1 1 1 4 4 4 4 3 1 0 0 2 6 6 6 6 6 7 7 2 2 3 3 2];
I apply some restrictions and removed some values from array of local maxima’s after that I received some new arrays.
Yposlocfiltered = [6 15 23];
idx = [4 6 3];
Yneglocfiltered = [2 9 20];
idx_neg = [1 1 2];
Where I will find idx(local maxima value) I will check if values behind and ahead are greater make a window.
Example
If I will find 4 and 4.5, 4.3 is greater than 4 include it in backward window
4.3 is greater than 4 include it in forward window.
I need to find range of values behind local maxima and ahead local maxima.
I have tried to write a code that’s works fine but issue is that it’s too long.
Can someone please provide me an idea to perform this action in minimal steps and in faster ways?
I have only provided code for positive local maxima’s as for negative local maxima code Is just replica of this.
Code:only for positive local maximas
clc
clear all
A = [1 0 1 4.3 4.5 5 4.3 3 0 0 0 2 6.2 6.3 7 6.2 7.4 8 7.2 1 2 3 4 2];
Yposlocfiltered = [ 6 15 23];
idx = [4 6 3];
Yneglocfiltered = [2 9 20];
idx_neg = [1 1 2];
for b = 1: numel(idx)
for c = 1:numel(A)
f = Yposlocfiltered(1,b)-c ;
g = Yposlocfiltered(1,b)+c ;
% if (f > 0 && g <= numel(X))
if(f > 0)
if (A(Yposlocfiltered(1,b)-c))> idx(1,b)
else
d= f+1;
z(b)= d;
backward_window = z;
break
end
end
end
end
for r = 1: numel(idx)
for s = 1:numel(A)
u = Yposlocfiltered(1,r)-s ;
v = Yposlocfiltered(1,r)+s ;
% if (f > 0 && g <= numel(X))
if(v <=numel(A))
if (A(Yposlocfiltered(1,r)+s))> idx(1,r)
else
w= v-1;
y(r)= w;
forward_window = y;
break
end
end
end
end
n=4
for i=1:length(backward_window)
range = backward_window(i): forward_window(i);
p = range
if n <= numel(p)
p = range(1:n)
A( p) = idx(i);
else
% if (size(range)<= 3)
A( p) = idx(i);
end
end
From the first look at your code, I believe you can combine your first two for loops into one.
sA = numel(A);
sI = numel(idx);
for i = 1:sI
f = Yposlocfiltered(i) - [1:sA];
g = Yposlocfiltered(i) + [1:sA];
f(f<1) = [];
g(g>sA) = [];
backward_window(i) = f(find(A(f) <= idx(i), 1)) + 1;
forward_window(i) = g(find(A(g) <= idx(i), 1)) - 1;
end
Here, you can use find to locate the element of an array matching the specified condition, i.e. g <= numel(X) or A(f) <= idx(i).
Your last loop which modifies A can also be integrated into the same loop, so you can have:
sA = numel(A);
sI = numel(idx);
n=4;
for i = 1:sI
f = Yposlocfiltered(i) - [1:sA];
g = Yposlocfiltered(i) + [1:sA];
f(f<1) = [];
g(g>sA) = [];
backward_window(i) = f(find(A(f) <= idx(i), 1)) + 1;
forward_window(i) = g(find(A(g) <= idx(i), 1)) - 1;
range = backward_window(i) : forward_window(i);
if n <= numel(range)
A(range(1:n)) = idx(i);
else
A(range) = idx(i);
end
end

How to draw Polynomial SVM modifying this code (Matlab)

well i'm new to this website and new when it comes to Matlab and Support-Vector-Machines and the teacher gave us this code :
clear all;
clc;
X_plus = load('example2_SVM.m');
[m,n] = size(X_plus);
ligne = m;
ligne_plus = 0;
for i=1:ligne
if(X_plus(i,n)==1)
ligne_plus = ligne_plus +1;
end
end
ligne_moins = ligne-ligne_plus;
colonne = n-1;
for i=1:ligne
y(1,i) = X_plus(i,colonne+1);
end
for i=1:ligne
for j=1:ligne
s=0;
for k=1:colonne
s = s + X_plus(i,k)*X_plus(j,k);
end
H(i,j) = y(i)*y(j)*s;
end
end
f = ones(1,ligne);
A = [];
b = [];
beq = zeros(1,1);
lb = zeros(1,ligne);
ub = zeros(1,ligne);
bornes_sup = 0.5;
for i=1:ligne
ub(i) = bornes_sup;
end
Aeq = y;
f = -f;
alpha = quadprog(H,f,A,b,Aeq,beq,lb,ub);
for k=1:colonne
s=0;
for i=1:ligne
s = s + alpha(i)*y(i)*X_plus(i,k);
end
w(k) = s;
end
for i=1:ligne
if(alpha(i)>0.001)
support = i;
break;
end
end
s = 0;
for k=1:colonne
for i=1:ligne
s = s + alpha(i)*y(i)*X_plus(support,k)*X_plus(i,k);
end
end
w0 = y(support) - s;
for i=1:m
if(X_plus(i,3)==1)
plot(X_plus(i,1),X_plus(i,2),'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',5)
else
plot(X_plus(i,1),X_plus(i,2),'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','r','MarkerSize',5)
end
hold on
end
hold on
x = -10:0.1:10;
y = -10:0.1:10;
if(w(2) ~= 0)
plot(x,-w(1)/w(2)*x-w0/w(2),'--rs','LineWidth',0.2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',1)
else
plot(x,-w0/w(1),'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',1)
end
and this is the example2_SVM.m :
1 0 +1
2 0 +1
1 1 +1
0 1 +1
0 2 +1
4 -1 +1
3 0 +1
3 3 -1
4 3 -1
5 3 -1
5 4 -1
5 2 -1
6 4 -1
4 2 -1
4 5 -1
so what are we doing in this code : is reading data from example2_SVM.m which contains negative and positive values to be separated using quadraprog function the result :
http://i.stack.imgur.com/Jkpsm.jpg
as you can see the points are linearly separable, my question is how to modify this code to draw a polynomial divider using this data :
1 2 +1
3 1 +1
3 2.5 +1
1 1 -1
2 3 -1
2 1 -1

MATLAB bsxfun or vectorization

I have been working on vectorizing my code mostly using bsxfun, but I came across a scenario that I can't quite crack. Here is a small sample of problem. I would like to remove the for loops in this code, but I am having a hard time with the tempEA line.
Index = [2; 3; 4;];
dTime = [25; 26; 27; 28; 25; 26; 27; 28; 27; 28];
dIndex = [3; 3; 3; 2; 1; 3; 2; 4; 4; 2];
aTime = [30; 38; 34; 39; 30; 38; 34; 39; 34; 39];
aIndex = [4; 2; 5; 4; 5; 4; 4; 2; 2; 4];
EA = zeros(numel(Index));
for i = 1:numel(Index)
for j = 1:numel(Index)
tempEA = aTime(Index(i) == dIndex(:,1) & Index(j) == aIndex(:,1));
if i == j
elseif tempEA > 0
EA(i,j) = min(tempEA);
else
EA(i,j) = 50;
end
end
end
The answer should look like this:
EA =
0 50 34
38 0 30
34 50 0
Thanks for help in advance.
This uses bsxfun; no loops. It assumes you don't have NaN's among your aTimevalues.
N = numel(Index);
ii = bsxfun(#eq, dIndex.', Index); %'// selected values according to each i
jj = bsxfun(#eq, aIndex.', Index); %'// selected values according to each j
[ igrid jgrid ] = ndgrid(1:N); %// generate all combinations of i and j
match = double(ii(igrid(:),:) & jj(jgrid(:),:)); %// each row contains the matches for an (i,j) combination
match(~match) = NaN; %// these entries will not be considered when minimizing
result = min(bsxfun(#times, aTime, match.')); %'// minimize according to each row of "match"
result = reshape(result,[N N]);
result(isnan(result)) = 50; %// set NaN to 50
result(result<=0) = 50; %// set nonpositive values to 50
result(1:N+1:end) = 0; %// set diagonal to 0
The line result(result<=0) = 50; is only necessary if your aTime can contain nonpositive values. Can it? Or is your elseif tempEA > 0 just a way of checking that tempEA is not empty?