I want to set precision for all element of a matrix. Below is what I did:
>>A
A =
0 1.0000 0 0 0 0
-137.0830 0 0 0 0 0
0 0 0 1.0000 0 0
365.5546 0 0 0 0 0
0 0 0 0 0 1.0000
365.5546 0 0 0 0 0
>> vpa(A,2)
ans =
[ 0, 1.0, 0, 0, 0, 0]
[ -144.0, 0, 0, 0, 0, 0]
[ 0, 0, 0, 1.0, 0, 0]
[ 377.0, 0, 0, 0, 0, 0]
[ 0, 0, 0, 0, 0, 1.0]
[ 377.0, 0, 0, 0, 0, 0]
The result is not my desire, it should be:
-137.08, 365.55, 365.55 in the first column.
Please help to suggest me how to get it.
Thank you so much!
You are not using vpa correctly. From the docs:
vpa(x,d) uses at least d significant digits
Nota that it says at least. That is why you still get 377 when you only asked for 2 significant digits.
It seems that you don't know what significant digits are. From Wikipedia:
The significant figures of a number are digits that carry meaning
contributing to its measurement resolution. This includes all digits
except:
All leading zeros;
Trailing zeros when they are merely placeholders to indicate the scale of the number (exact rules are explained at identifying
significant figures); and
Spurious digits introduced, for example, by calculations carried out to greater precision than that of the original data, or measurements
reported to a greater precision than the equipment supports.
So you want this
>> vpa(365.5546, 5)
ans =
365.55
Now, to be consistent, you need to find out what is the maximum of your matrix and compute the desired number of significant digits from there.
max_number = floor(log10(max(abs(A(:))+1)) + 1);
decimals = 2;
vpa(A, max_number + decimals)
Here, max_number is the maximum number of integer digits that your matrix has, and decimals is the number of decimal places that you want to have.
The second input to vpa is the number of significant digits which is not the same as the number of values after the radix point. The number -137.08 actually has five signficant digits so you'll want to use a second input of 5
vpa(A, 5)
% [ 0, 1.0, 0, 0, 0, 0]
% [ -137.08, 0, 0, 0, 0, 0]
% [ 0, 0, 0, 1.0, 0, 0]
% [ 365.55, 0, 0, 0, 0, 0]
% [ 0, 0, 0, 0, 0, 1.0]
% [ 365.55, 0, 0, 0, 0, 0]
vpa(x,d) :
d is significant digits, not decimal places.
pvalue = vpa(-137.0830);
twopvalue = vpa(-137.0830,2);
% pvalue = -137.08299999999999840838427189738
% twopvalue = -144.0
If you want to get -137.08, 365.55, 365.55 in the first column. You can use roundn(A,-2)
roundn(A,-2)
ans =
0 1.0000 0 0 0 0
-137.0800 0 0 0 0 0
0 0 0 1.0000 0 0
365.5500 0 0 0 0 0
0 0 0 0 0 1.0000
365.5500 0 0 0 0 0
Related
I try to run this example, which uses ANN Toolbox for Scilab
https://burubaxair.wordpress.com/2014/03/12/artificial-neural-networks-in-scilab/
This is code:
T = [
1 1 1 1 1
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
]';
U = [
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
0 1 1 1 0
]';
N = [35 10 2];
W = ann_FF_init(N);
x = [1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
0, 1, 1, 1, 0]';
t_t = [1 0]';
t_u = [0 1]';
t = [t_t, t_u];
lp = [0.01, 1e-4];
epochs = 3000;
W = ann_FF_Std_batch(x,t,N,W,lp,epochs);
y = ann_FF_run(x,N,W)
disp(y)
But i receive an error:
-->exec('D:\Учёба\Задачи\Recognition.sce', -1)
!--error 15
Подматрица задана некорректно (Submatrix is incorrect).
at line 37 of function ann_FF_grad_BP called by :
at line 25 of function ann_FF_Std_batch called by :
W = ann_FF_Std_batch(x,t,N,W,lp,epochs);
at line 33 of exec file called by :
exec('D:\Учёба\Задачи\Recognition.sce', -1)
An error may be in T and U matrix, but i don't understand why. Could you tell be what i do wrong? Thank you!
You've made 2 errors in your code:
You should not mix testing and training sets.
Testing input must be a single column.
Your first error was x = [ 1.... ] because it contained a single image, whereas you specified in N that you had two output neurons.
As stated in the example, you should have x = [T,U];
Your second error was to give x as a test to ann_FF_run. This function takes test input as a single column. But since you trained your NN with x just before it was a 5x7 matrix. Just change it to a column vector.
Here a corrected and commented code :
T = [...
1 1 1 1 1 ...
0 0 1 0 0 ...
0 0 1 0 0 ...
0 0 1 0 0 ...
0 0 1 0 0 ...
0 0 1 0 0 ...
0 0 1 0 0 ...
]';
U = [...
1 0 0 0 1 ...
1 0 0 0 1 ...
1 0 0 0 1 ...
1 0 0 0 1 ...
1 0 0 0 1 ...
1 0 0 0 1 ...
0 1 1 1 0 ...
]';
// setting the traing set of two image
xtrain = [T,U];
// so each image as 35 pixels so N(1) is 35
// and we have two images so N($) is 2
N = [35 10 2];
// training the NN
W = ann_FF_init(N);
// The expected response for T : 1 for T, 0 for U
t_t = [1 0]';
// The expected response for T : 1 for T, 0 for U
t_u = [0 1]';
// the overall response
t = [t_t, t_u];
// some parameters
lp = [0.01, 1e-4];
epochs = 3000;
// getting the weight of the trained NN
W = ann_FF_Std_batch(xtrain,t,N,W,lp,epochs);
// testing the traing set.
y = ann_FF_run(xtrain,N,W)
disp('Testing the traing set')
disp(y) //should get something close to t ~ [1 0 ; 0 1]
// testing a distord U
xtest1 = matrix([1, 0, 0, 0, 1;
1, 1, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
1, 0, 0, 0, 1;
0, 1, 1, 1, 1]',-1,1);
y = ann_FF_run(xtest1,N,W)
disp('Testing a distored U')
disp(y) //should get something close to t_u ~ [0 1]
//testing something different from T and U. should get nothing
xtest2 = matrix([1, 0, 0, 0, 1;
1, 1, 0, 0, 1;
1, 0, 1, 0, 1;
1, 0, 1, 0, 1;
0, 0, 1, 1, 1;
0, 0, 1, 0, 1;
0, 1, 1, 1, 1]',-1,1);
y = ann_FF_run(xtest2,N,W)
disp('Testing something neither T nor U')
disp(y)
and the output from scilab's console
Testing the traing set
0.8538757 0.1075397
0.1393287 0.8957439
Testing a distored U
0.1078667
0.9007755
Testing something neither T nor U
0.3433933
0.6306797
Let's say i have an array:
A = [0, -2, 0, 0, -3, 0, -1, 0];
And I want to see if it can fit any of the patterns below:
B = [1, 1, 1 , 0, 0 , 0 , 0, 0,];
C= [1, 1, 0 , 1, 0 , 0 , 0, 0,]
D= [0, 1, 0 , 1, 0 , 1 , 0, 0];
Where 1 means that the number is unique and 0 means that the number remains the same until another 1 is met but is different from the number before.. Here are some examples:
[-3, -2, -1, 0, 0, 0, 0, 0]; --- A matches B.
[-3, -2, -1, -1, 0, 0, 0, 0]; -- This matches C
[-3, -3, 3, 3, -2, -2, 0, 0]; -- This matches D
Is there any Matlab function for this or must I think up my own way? Any advice, I am very new to Matlab.
There is something wrong about your rule, you seem to treat 0 as a special case (i.e. it is not a number or something).
you could just do something like this:
A=[-3, -2, -1, -1, 0, 0, 0, 0];
[ia ib] = unique(A);
R = zeros(1,8);
R(ib) = 1
>> R =
1 1 0 1 0 0 0 1
and match this, this is assuming that you treat 0 as a number just as you state in the rule.
If you wanted 0 to be special case, you need to :
A=[-3, -2, -1, -1, 0, 0, 0, 0];
[ia ib] = unique(A);
ib(ia==0)=[];
R = zeros(1,8);
R(ib) = 1
>> R =
1 1 0 1 0 0 0 0
and simply match this vector to your B,C,D etc. The second method matches your desired answer, but does not match the rule you state. The first method matches the rule you state but not your desired output.
=============EDIT============
I am on 2010b and some time along the time line, the way unique works changed, you now need to add legacy if you are using any version above 2012b.
Sorry I forgot to mention this:
just change it to :
[ia ib] = unique(A,'legacy');
and it should work fine.
Here is the initial question:
About the output value range of LeGall 5/3 wavelet
Today I found actually the transform can be seen as a matrix multiplication. It is easy to calculate the wavelet coefficients as a matrix (in order to estimate the value, all the Rounded down action is ignored which will not affect the estimation of the max value).
The 1st level of DWT2 has two steps which is to perform the LeGall 5/3 filter on two directions. If we see the I as the input 8*8 matrix and A as the wavelet coefficients matrix.
For the horizontal direction:
output1 = I.A
Then the vertical direction is calculated:
In fact, it can be represented as output2 = output1'.A (use the transpose of output1 to multiply A again) which will get the transpose of the result we want.
Transpose the output2.
output_lvl1 = output2' = (output1'.A)' = ((I.A)'.A)' = (A'.I'.A)'=A'.I.A (I put details here to make it clear without the math symbol...)
And the 2nd level of the wavelet is only performed at the LL area which is the output_lvl1(1:4,1:4). And basically the process is the same (let the coefficients matrix represented as B).
Here is the coefficients of the matrix A and B based on my calculation (hope it is correct...)
A = [0.75 -0.125 0 0 -0.5 0 0 0;
0.5 0.25 0 0 1 0 0 0;
-0.25 0.75 -0.125 0 -0.5 -0.5 0 0;
0 0.25 0.25 0 0 1 0 0;
0 -0.125 0.75 -0.125 0 -0.5 -0.5 0
0 0 0.25 0.25 0 0 1 0;
0 0 -0.125 0.625 0 0 -0.5 -1;
0 0 0 0.25 0 0 0 1];
B = [0.75 -0.125 -0.5 0;
0.5 0.25 1 0;
-0.25 0.75 -0.5 -1;
0 0.125 0 1];
And now the question became:
1. if we know A and the range of Input(matrix I) which is -128 to +127, what is the value range of output_lvl1 = A'.I.A?
if we use the output_lvl1(1:4,1:4) as input I2, what is value range of B'.I2.B?
I really need some math help here. Thank you in advance.
Well finally I found a way to solve this.
SymPy lib is what I really need.
As the max value could only be possible in results of B'.I2.B. So a program will do this.
from sympy import *
def calcu_max(strin):
x=0
strin1 = str(strin).replace('*',' ').replace('+',' ').replace('-',' ')
strin1 = strin1.split(' ')
for ele in strin1:
if '[' in ele or ']' in ele or ele =='':
continue
x = x + float(ele)
return x
DWT1 = Matrix(8, 8, [0.75, -0.125, 0, 0,-0.5, 0, 0, 0, 0.5, 0.25, 0, 0, 1, 0, 0, 0, -0.25, 0.75, -0.125, 0, -0.5, -0.5, 0, 0, 0, 0.25, 0.25, 0, 0, 1, 0, 0, 0,-0.125, 0.75, -0.125, 0, -0.5, -0.5, 0, 0, 0, 0.25, 0.25, 0, 0, 1, 0, 0, 0, -0.125, 0.625, 0, 0, -0.5, -1, 0, 0, 0, 0.25, 0, 0, 0, 1])
Input1 = MatrixSymbol('A',8,8)
DWT1_t = Transpose(DWT1)
output_lvl1_1d = DWT1_t*Input1
output_lvl1_2d = output_lvl1_1d* DWT1
#print 'output_lvl1_2d[0,0]: '
#print simplify(output_lvl1_2d[0,0])
#bulit 2nd lvl input from the lvl1 output (1:4,1:4)
input_lvl2 = output_lvl1_2d[0:4,0:4]
DWT2 = Matrix(4, 4, [0.75, -0.125, -0.5, 0, 0.5, 0.25, 1, 0, -0.25, 0.75, -0.5, -1, 0, 0.125, 0, 1])
DWT2_t = Transpose(DWT2)
output_lvl2_1d = DWT2_t*input_lvl2
output_lvl2_2d = output_lvl2_1d * DWT2
#Lvl 2 calculate max
max_lvl2 = zeros(4,4)
for i in range(4):
for j in range(4):
max_lvl2[i,j]=128.0*calcu_max(simplify(output_lvl2_2d[i,j]))
print str(i)+' '+str(j)+' '+str(max_lvl2[i,j])
#print max_lvl2[i,j]
print max_lvl2
Well, here is the result (putting all possible max values in one matrix, and min values are correspondingly negative):
[338.000000000000, 266.500000000000, 468.000000000000, 468.000000000000],
[266.500000000000, 210.125000000000, 369.000000000000, 369.000000000000],
[468.000000000000, 369.000000000000, 648.000000000000, 648.000000000000],
[468.000000000000, 369.000000000000, 648.000000000000, 648.000000000000]
Then 648 is what I am looking for.
Is there any inline command to generate shifted identity matrix in MATLAB?
A=[ ...
0, 1, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 1, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 1, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 1, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 1, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 1, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 1, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 1, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
combination of circshift and eye is good however it needs another command to fix it. Any simpler way? (with just one simple syntax)
Try using a diag call in combination with ones. For your case, you have a 10 x 10 identity matrix and want to shift the diagonal to the right by 1.
>> n = 10;
>> shift = 1;
>> A = diag(ones(n-abs(shift),1),shift)
A =
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
The above code works by first declaring a column vector of all 1s, but we would need n-abs(shift) of them as moving to the right would mean that we would require less 1s to fill things up (more on this later). n-abs(shift) also corresponds to the total number of rows/columns of your matrix and subtracting out as many times you are shifting towards the right. Next, you can use diag where the first parameter is a column vector which creates a zero matrix and places the column vector as coefficients along the diagonal of this matrix. The second parameter (shift in your case) allows you to offset where to place this column. Specifying a positive value means to move the diagonals towards the right, and in our case we are moving this to the right by shift, and hence our output results. As you are essentially truncating the vector for each position towards the right you are moving, you would need to decrease the number of 1s in your vector by this much.
Up to now, I haven't explained why the abs call to shift is required in the last line of code. The reason why the abs call is required is to accommodate for negative shifts. If we didn't have the abs call in the third line of code, n-shift would essentially be adding more 1s to the vector and would thus expand our matrix beyond n x n. Because moving the diagonals to the left also decreases the amount of 1s seen in the result, that's why the abs call is required but you'll notice that the shift constant is left untouched in the second parameter of diag.
Here's a demonstration with a negative shift, shift = -1, and still maintaining the size to be 10 x 10:
A =
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
You can get the desired output with a single call to bsxfun -
n = 10
shift = 1
A = bsxfun(#eq,[1:n].',1-shift:n-shift)
Since you are basically creating a sparse matrix, alternatively you can use sparse -
n = 10
shift = 1
A = full(sparse(1:n-shift,1+shift:n,1,n,n))
way late in this game but let us not forget the simplest solution using linear indexing:
n=10; a=zeros(n);
a(n+1:n+1:end)=1
obviously, that just solves the shift=1 case, but you get the point...
You can use circshift and fix the matrix before passing it to the function:
>> shift = 1;
>> N=10;
>> A=circshift(diag(1:N>shift),-shift)
A =
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
1:N>shift will be 0 for fist shift number of places and 1 for the remaining.
Here is Another Alternative: (little similar to bsxfun Approach by Divakar)
n=10;
shift = 1;
c = repmat(1-shift:n-shift,n,1,1);
r = repmat((1:n).',1,n,1);
out = r == c
This could also be a one-liner:
out = repmat((1:n).',1,n,1) == repmat(1-shift:n-shift,n,1,1)
Here is another one (also works with negative shifts)
rot90(blkdiag(zeros(abs(shift)),rot90(eye(n))),sign(shift))
syms t theta chy sy real;
A = [0 0 0 0; 0 -theta -0.5 0;0 -0.5 0 0;0 0 0 0];
B = [0 theta/2 0.5 0; theta/2 0 0 0;0.5 0 0 0;0 0 0 0];
C = [0 (1-(theta^2))/2 -(theta/2) 0;(1-(theta^2))/2 0 0 0; -(theta/2) 0 0 0;0 0 0 0];
D = sym(zeros(4,4));
CS = cat(3,A,B,C,D);
Now when I type
>> CS(:,1,3)
ans =
[ 0, 1/2 - theta^2/2, -theta/2, 0]
[ 1/2 - theta^2/2, 0, 0, 0]
[ -theta/2, 0, 0, 0]
[ 0, 0, 0, 0]
>> CS(:,:,3)
ans =
[ 0, 1/2 - theta^2/2, -theta/2, 0]
[ 1/2 - theta^2/2, 0, 0, 0]
[ -theta/2, 0, 0, 0]
[ 0, 0, 0, 0]
which is supposed to be different from CS(1,1,3) and CS(:,1,3).
>> CS(1,1,3)
ans =
[ 0, 1/2 - theta^2/2, -theta/2, 0]
[ 1/2 - theta^2/2, 0, 0, 0]
[ -theta/2, 0, 0, 0]
[ 0, 0, 0, 0]
All give the same values. How do I access the first value in that particular matrix. I don't want to use the A/B/C matrices.
It is working fine for me (R2011a):
syms t theta chy sy real;
A = [0 0 0 0; 0 -theta -0.5 0;0 -0.5 0 0;0 0 0 0];
B = [0 theta/2 0.5 0; theta/2 0 0 0;0.5 0 0 0;0 0 0 0];
C = [0 (1-(theta^2))/2 -(theta/2) 0;(1-(theta^2))/2 0 0 0; -(theta/2) 0 0 0;0 0 0 0];
D = sym(zeros(4,4));
CS = cat(3,A,B,C,D);
>> CS(:,1,3)
ans =
0
1/2 - theta^2/2
-theta/2
0
>> CS(:,:,3)
ans =
[ 0, 1/2 - theta^2/2, -theta/2, 0]
[ 1/2 - theta^2/2, 0, 0, 0]
[ -theta/2, 0, 0, 0]
[ 0, 0, 0, 0]
>> CS(1,1,3)
ans =
0
EDIT: As you see, R2011a gives the expected results. However, I've just checked it on R2010a (the OP's version) and also got your results... so you probably need an upgrade :)