Response Surface Methodology-Third Order - matlab

I am trying to use RSM and calculate 3rd order polynomials.for quadratic below is given in Matlab Help:
stats = regstats(rsmOutput,rsmMatrix,'quadratic','beta');
b = stats.beta; % Model coefficients
How can I calculate 3rd order coefficients? My reason is that with quadratic I have rsquare of 93% and my observed responses is third order.

For
stats = regstats(y,X,model,whichstats)
the 'model' can be a matrix of model terms accepted by the 'x2fx' function. See x2fx for a description of this matrix and for a description of the order in which terms appear. You can use this matrix to specify other models including ones without a constant term.

modelMatrix = [0 0 0;
1 0 0;
0 1 0;
0 0 1;
1 1 0;
1 0 1;
0 1 1;
2 0 0;
0 2 0;
0 0 2;
1 1 1;
2 1 0;
2 0 1;
1 2 0;
1 0 2;
0 2 1;
3 0 0;
0 3 0;
0 0 3];
stats = regstats(rsmOutput,rsmMatrix,modelMatrix,'beta');

Related

How to fix up the error in matrix dimensions in MATLAB R2016b

I am working on a MATLAB code that involves deep learning using neural networks.
The image or the data is being fed in the form of matrices.
But I am getting an error "Matrix dimensions must agree".
Can Someone please help me with this problem?
I tried to solve this problem by using .* in stead of matrix multiplication * but the method didn't work.
Function Deeplearningoriginal:
function [w1,w2,w3,w4] = Deeplearningoriginal(w1,w2,w3,w4,input_Image,correct_output)
alpha=0.01;
N=5;
for k = 1:N
input_Image = reshape( input_Image( :, :,k ),25 ,1);
input_of_hidden_layer1 = w1* input_Image;
output_of_hidden_layer1 = ReLU(input_of_hidden_layer1);
input_of_hidden_layer2 = w2* output_of_hidden_layer1;
output_of_hidden_layer2 = ReLU( input_of_hidden_layer2);
input_of_hidden_layer3 = w3* output_of_hidden_layer2;
output_of_hidden_layer3 = ReLU(input_of_hidden_layer3);
input_of_output_node = w4* output_of_hidden_layer3;
final_output = Softmax(input_of_output_node);
correct_output_transpose = correct_output(k,:);
error = correct_output_transpose - final_output;
delta4 = error;
error_of_hidden_layer3 = w4'* delta4;
delta3 = (input_of_hidden_layer3>0).*error_of_hidden_layer3;
error_of_hidden_layer2 = w3'* delta3;
delta2 = (input_of_hidden_layer2>0).* error_of_hidden_layer2;
error_of_hidden_layer1 = w2'*delta2;
delta1 = (input_of_hidden_layer1>0).* error_of_hidden_layer1;
adjustment_of_w4 = alpha*delta4*output_of_hidden_layer3';
adjustment_of_w3 = alpha*delta3*output_of_hidden_layer2';
adjustment_of_w2 = alpha*delta2*output_of_hidden_layer1';
adjustment_of_w1 = alpha*delta1*reshaped_input_image';
w1 = w1 + adjustment_of_w1;
w2 = w2 + adjustment_of_w2;
w3 = w3 + adjustment_of_w3;
w4 = w4 + adjustment_of_w4;
end
end
Training network:
input_Image = zeros (5,5,5);
input_Image(:,:,1) = [ 1 0 0 1 1;
1 1 0 1 1;
1 1 0 1 1;
1 1 0 1 1;
1 0 0 0 1;
];
input_Image(:,:,2) = [ 0 0 0 0 1;
1 1 1 1 0;
1 0 0 0 1;
0 1 1 1 1;
0 0 0 0 0;
];
input_Image(:,:,3) = [ 0 0 0 0 1;
1 1 0 0 1;
1 0 1 0 1;
0 0 0 0 0;
1 1 1 0 1;
];
input_Image(:,:,4) = [ 1 1 1 0 1;
1 1 0 0 1;
1 0 1 0 1;
0 0 0 0 0;
1 1 1 0 1;
];
input_Image(:,:,5) = [ 0 0 0 0 0;
0 1 1 1 1;
0 0 0 0 1;
1 1 1 1 0;
0 0 0 0 1;
];
correct_output = [ 1 0 0 0 0;
0 1 0 0 0;
0 0 1 0 0;
0 0 0 1 0;
0 0 0 0 1;
];
w1 = 2* rand(20,25) -1;
w2 = 2* rand(20,20) -1;
w3 = 2* rand(20,20) -1;
w4 = 2* rand(5,20) -1;
for epoch = 1:100
[w1,w2,w3,w4] = Deeplearningoriginal(w1,w2,w3,w4,input_Image,correct_output);
end
I expected this code to run but because of the error I am not able to proceed.
The problem is the reshape (actually, two problems). After the
input_image = reshape(input_image(:,:,k), 25,1);
input_image is an array with 25 rows and 1 column, whereas w2, w3, and w4 have only 20 columns. To do the matrix multiplication A*B, A must have as many columns as B has rows.
The other problem with the reshape as written is that after the first pass through the loop, input_image is no longer a 5x5x5 array, it is a 25x1 array that contains only the elements of input_image(:,:,1). It is necessary to use a different name on the left-hand-side of the assignment (and throughout the rest of the loop) to avoid loosing the content of input_image.
Hope this helps,
JAC

How to permute elements of a vector by another vector to obtain a matrix of permutations

I want to obtain all the possible permutations of one vector elements by another vector elements. For example one vector is A=[0 0 0 0] and another is B=[1 1]. I want to replace the elements of A by B to obtain all the permutations in a matrix like this [1 1 0 0; 1 0 1 0; 1 0 0 1; 0 1 1 0; 0 1 0 1; 0 0 1 1]. The length of real A is big and I should be able to choose the length of B_max and to obtain all the permutations of A with B=[1], [1 1], [1 1 1],..., B_max.
Thanks a lot
Actually, since A and B are always defined, respectively, as a vector of zeros and a vector of ones, this computation is much easier than you may think. The only constraints you should respect concerns B, which shoud not be empty and it's elements cannot be greater than or equal to the number of elements in A... because after that threshold A will become a vector of ones and calculating its permutations will be just a waste of CPU cycles.
Here is the core function of the script, which undertakes the creation of the unique permutations of 0 and 1 given the target vector X:
function p = uperms(X)
n = numel(X);
k = sum(X);
c = nchoosek(1:n,k);
m = size(c,1);
p = zeros(m,n);
p(repmat((1-m:0)',1,k) + m*c) = 1;
end
And here is the full code:
clear();
clc();
% Define the main parameter: the number of elements in A...
A_len = 4;
% Compute the elements of B accordingly...
B_len = A_len - 1;
B_seq = 1:B_len;
% Compute the possible mixtures of A and B...
X = tril(ones(A_len));
X = X(B_seq,:);
% Compute the unique permutations...
p = [];
for i = B_seq
p = [p; uperms(X(i,:).')];
end
Output for A_len = 4:
p =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 1 0 0
1 0 1 0
1 0 0 1
0 1 1 0
0 1 0 1
0 0 1 1
1 1 1 0
1 1 0 1
1 0 1 1
0 1 1 1

Make a one simple code from several similar code

Hello everybody I have a very simple problem, I have too many data y, p and r. So I want to calculate it in a single code.
This an example of my code if I breakdown into separate code
y1=45
y2=56
y3=67
p1=34
p2=45
p3=56
r1=23
r2=34
r3=45
Ryaw1=[cosd(y1) -sind(y1) 0;
sind(y1) cosd(y1) 0;
0 0 1]
Rpitch1=[cosd(p1) 0 sind(p1);
0 1 0;
-sind(p1) 0 cos(p1)]
Rroll1=[1 0 0;
0 cosd(r1) -sind(r1);
0 sind(r1) cosd(r1)]
R1=Ryaw1*Rpitch1*Rroll1
Coordinate1=R1*X0
Ryaw2=[cosd(y2) -sind(y2) 0;
sind(y2) cosd(y2) 0;
0 0 1]
Rpitch2=[cosd(p2) 0 sind(p2);
0 1 0;
-sind(p2) 0 cos(p2)]
Rroll2=[1 0 0;
0 cosd(r2) -sind(r2);
0 sind(r2) cosd(r2)]
R2=Ryaw2*Rpitch2*Rroll2
Coordinate2=R2*X0
Ryaw3=[cosd(y3) -sind(y3) 0;
sind(y3) cosd(y3) 0;
0 0 1]
Rpitch3=[cosd(p3) 0 sind(p3);
0 1 0;
-sind(p3) 0 cos(p3)]
Rroll3=[1 0 0;
0 cosd(r3) -sind(r3);
0 sind(r3) cosd(r3)]
R3=Ryaw3*Rpitch3*Rroll3
Coordinate3=R3*X0
Coordinate=[Cooedinate1 Coordinate2 Coordinate3]
The goals is to find "Coordinate" (in matrix - combined from Coordinate1, Coordinate2, Coordinate3, .... ,Coordinate..) from every y, p and r data with the same "X0" as a single primary data for calculation.
Sorry for my bad english,
Thanks :)
Use vectors and matrices instead of individual scalars. These are indexed in almost the same way as you had before, i.e. y1 becomes y(1).
Then you can easily loop over the code 3 times and save the repetition.
See my commented code below.
% Define some X0. This should be a column vector.
X0 = [1; 2; 3];
% Make y,p,r into 3 element vectors
y = [45 56 67];
p = [34 45 56];
r = [23 34 45];
% Make R, Ryaw, Rpitch and Rroll 3x3x3 matrices
R = zeros(3,3,3);
Ryaw = zeros(3,3,3);
Rpitch = zeros(3,3,3);
Rroll = zeros(3,3,3);
% Make Coordinate a 3x3 matrix
Coordinate = zeros(3,3);
% Loop k from 1 to 3
% For each 3x3x3 matrix, the kth 3x3 matrix is equivalent to your Ryawk, Rpitchk, Rrollk, Rk
for k = 1:3
Ryaw(:,:,k) = [cosd(y(k)) -sind(y(k)) 0
sind(y(k)) cosd(y(k)) 0
0 0 1];
Rpitch(:,:,k)= [cosd(p(k)) 0 sind(p(k))
0 1 0
-sind(p(k)) 0 cos(p(k))];
Rroll(:,:,k) = [1 0 0
0 cosd(r(k)) -sind(r(k))
0 sind(r(k)) cosd(r(k))];
R(:,:,k) = Ryaw(:,:,k)*Rpitch(:,:,k)*Rroll(:,:,k);
Coordinate(:,k) = R(:,:,k)*X0;
end
disp(Coordinate)

What is the meaning of these matrices in Perona & Malik filter

I was searching for the implementation of Perona & Malik filter in Matlab, when i found this link "the link has an implementation for Perona and Malik filter using Matlab"
but there is the matrices that i didn't understand what is the use of them:
% 2D convolution masks - finite differences.
hN = [0 1 0; 0 -1 0; 0 0 0];
hS = [0 0 0; 0 -1 0; 0 1 0];
hE = [0 0 0; 0 -1 1; 0 0 0];
hW = [0 0 0; 1 -1 0; 0 0 0];
hNE = [0 0 1; 0 -1 0; 0 0 0];
hSE = [0 0 0; 0 -1 0; 0 0 1];
hSW = [0 0 0; 0 -1 0; 1 0 0];
hNW = [1 0 0; 0 -1 0; 0 0 0];
any one has an idea what are these matrices, or what do they mean?
These are the kernels for getting the gradients in North, East, South and West directions, and also for dialgonals (NE = North East etc.).
hN = [0 1 0; 0 -1 0; 0 0 0];
or nicely formatted:
0 1 0
0 -1 0
0 0 0
Convolution of this kernel with an image yields the gradient in "North direction". Basically, this is a mask of factors that you apply to all your 3x3 areas of your image and since all except two entries are 0, what you get is the pixel at the top minus that in the center, thus the gradient in y-direction.

Identify a block diagonal matrix in Matlab?

I need to check if a matrix is a diagonal block matrix or not. Is there any easy way to check it? Especially, that would be perfect if a simple function such as isdiag()exist.
A function isdiag actually exists for R2014a and later. It works for square and non-square matrices. For earlier versions of Matlab see an alternative below.
Here is a quick demo:
A = [1 0 0;
0 2 0;
0 0 3];
isdiag(A)
B = [1 0 0;
0 2 3;
0 0 4];
isdiag(B)
C = [1 0 0 0;
0 2 0 0;
0 0 3 0];
isdiag(C)
D = [1 0 0 0;
0 2 0 3;
0 0 4 0];
isdiag(D)
... and we get:
ans =
1
ans =
0
ans =
1
ans =
0
In R2014a and later, you can as well use isbanded with a lower and upper bandwidth of 0. This will give you exactly the same result:
isbanded(M,0,0)
For versions before R2014a, you can use a combination of find and all:
[t1,t2] = find(M);
res = all(t1==t2)