Some problems with accessing individual elements in MATLAB - matlab

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 :)

Related

reshaping (1,103,10) into (103,10) array

I have as input
Y =[[[0 0 0 ..., 0 0 0]
[1 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 1 0 ..., 0 0 0]
[0 0 1 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]]
Meaning the shape is (1,103,10)
What I want is an array of shape (103,10)
So my array should look like:
Y =[[0 0 0 ..., 0 0 0]
[1 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 1 0 ..., 0 0 0]
[0 0 1 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
How can we do this?

Neural Network Recognising in Scilab

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

MATLAB graph to adjacency matrix?

I can create a graph using the matlab functions but I need to create an adjacency matrix from the graph now.
Here is how I created the graph:
s = roadmap.edges(:,1); % vector [1, 2, 3, 4, ...]
t = roadmap.edges(:,2); % vector [1, 2, 3, 4, ...]
weights = roadmap.edge_lengths'; % vector [1, 2, 3, 4, ...]
G = graph(s, t, weights);
I need to create a adjacency matrix from this data and I want it to look like this:
G = [0 3 9 0 0 0 0;
0 0 0 7 1 0 0;
0 2 0 7 0 0 0;
0 0 0 0 0 2 8;
0 0 4 5 0 9 0;
0 0 0 0 0 0 4;
0 0 0 0 0 0 0;
];

Set precision an element in Matlab matrix

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

Count frequencies of pairs in matrix matlab

I have matrix X , mX2, I want to result a matrix S of size
size(unique(X(:,2),1) X size(unique(X(:,2),1)
for each S(i,j) I want to count how many times i,j appeared together.
for example:
X = [1 11 ;
2 11;
3 11;
5 23;
6 23;
1 23;
9 24;
9 25;
3 23;
10 23]
unique(X(:,2))
11
23
24
25
S should be:
0 2 0 0
0 0 0 0
0 0 0 1
0 0 0 0
(I don't care about diagonals, and it could either have them or not, also,S could be symmetric).
S(1,2) = 2
because 11 and 23 (which are in position 1,2) appeared together twice (i.e with the same value in X(:,1)).
Thanks
This is one way of doing it:
[~, ~, n1] = unique(X(:,1));
[~, ~, n2] = unique(X(:,2));
B = accumarray([n2 n1],1);
S = B*B';
This gives the full matrix:
>> S
S =
3 2 0 0
2 5 0 0
0 0 1 1
0 0 1 1
To remove the diagonal and lower triangle you can use
S = triu(B*B',1);
which yields
>> S
S =
0 2 0 0
0 0 0 0
0 0 0 1
0 0 0 0
Try the following:
% convert each columns to indices starting from 1
[a,~,aa] = unique(X(:,1));
[b,~,bb] = unique(X(:,2));
% group occurences of col2 according to values of col1
C = accumarray(aa, bb, [], #(x){x});
% keep only occurences of two or more values
C = C(cellfun(#numel,C) > 1);
% in case of three or more values co-occured, generate all pairs
C = cellfun(#(v) nchoosek(v,2), C, 'UniformOutput',false);
% concatenate all pairs
C = cell2mat(C);
% build count matrix
C = sparse(C(:,[1 2]), C(:,[2 1]), 1);
C = full(C);
The result in this case (obviously a symmetric matrix):
>> C
C =
0 2 0 0
2 0 0 0
0 0 0 1
0 0 1 0
or pretty-printed with row/column headers:
>> [{[]} num2cell(b'); num2cell(b) num2cell(C)]
ans =
[] [11] [23] [24] [25]
[11] [ 0] [ 2] [ 0] [ 0]
[23] [ 2] [ 0] [ 0] [ 0]
[24] [ 0] [ 0] [ 0] [ 1]
[25] [ 0] [ 0] [ 1] [ 0]