QB64: "Duplicate Definition" error when using variable type suffixes with '$DYNAMIC - qbasic

Why is it, in QB64, that
'$dynamic
x&(5) = x&(5) + 1
x&(5) = x&(5) + 1
x&(5) = x&(5) + 1
x&(5) = x&(5) + 1
x&(5) = x&(5) + 1
works fine, while
'$dynamic
for g = 1 to 5
x&(5) = x&(5) + 1
next g
gives a "duplicate definition" error at x&(5) = x&(5) + 1?

Related

MATLAB initializing variables in next for loop

x=[3 1 1 -5 -2 0 1 2 -2 2];
A=[4 2 6; 0 1 -3; -2 5 -2];
B=[-2 3 2; 1 5 5; -3 1 0];
sum=0;
for i=2:3
sum_j=0;
for j=1:2
sum_j=sum_j+A(1,j)*B(j,i);
end
sum=sum+A(2,i)*sum_j;
end
fprintf('(c) %g\n',sum);
>> (c) -32
-32 is a correct answer. However, if I initialize sum_j=0 outside of the the loop, it returns a different value.
sum=0;
sum_j=0;
for i=2:3
for j=1:2
sum_j=sum_j+A(1,j)*B(j,i);
end
sum=sum+A(2,i)*sum_j;
end
fprintf('(c) %g\n',sum);
>> (c) -98
Can anyone explain why this is happening?
In your first code sum_j gets reinitialized in the loop for i and in second code sum_j carry the value in each loop. Here is a simulation of your codes
first code:
sum = 0
i=2:
sum_j = 0
i=2,j=1
sum_j = 0 + 4*3 = 12
i=2,j=2
sum_j = 12 + 2*5 = 22
sum = 0 + 1*22 = 22
i=3
sum_j = 0
i=3,j=1
sum_j = 0 + 4*2 = 8
i=3,j=2
sum_j = 8 + 2*5 = 18
sum = 22 + -3*18 = -32
Second code
sum = 0
sum_j = 0
i=2:
i=2,j=1
sum_j = 0 + 4*3 = 12
i=2,j=2
sum_j = 12 + 2*5 = 22
sum = 0 + 1*22 = 22
i=3
i=3,j=1
sum_j = 22 + 4*2 = 30
i=3,j=2
sum_j = 30 + 2*5 = 40
sum = 22 + -3*40 = -98

Matlab: Nonlinear Eq: error

I have the code from a previous question, however, this one is similar, just more equations added. However, I get an error and I'm not sure how to fix it.
Link to my previous question: Matlab: Nonlinear equation solver
function F = fcn(x)
F=[x(6) + x(7) + x(8) + x(9) + x(10) - 2 ;
x(6)*x(1) + x(7)*x(2) + x(8)*x(3) + x(9)*x(4) + x(10)*x(5) ;
x(6)*x(1)^2 + x(7)*x(2)^2 + x(8)*x(3)^2 + x(9)*x(4)^2 + x(10)*x(5) - 2/3 ;
x(6)*x(1)^3 + x(7)*x(2)^3 + x(8)*x(3)^3 + x(9)*x(4)^3 + x(10)*x(5) ;
x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5) -2/5 ;
x(6)*x(1)^5 + x(7)*x(2)^5 + x(8)*x(3)^5 + x(9)*x(4)^5 + x(10)*x(5) ;
x(6)*x(1)^6 + x(7)*x(2)^6 + x(8)*x(3)^6 + x(9)*x(4)^6 + x(10)*x(5) -2/7 ;
x(6)*x(1)^7 + x(7)*x(2)^7 + x(8)*x(3)^7 + x(9)*x(4)^7 + x(10)*x(5) ;
x(6)*x(1)^8 + x(7)*x(2)^8 + x(8)*x(3)^8 + x(9)*x(4)^8 + x(10)*x(5) -2/9 ;
x(6)*x(1)^9 + x(7)*x(2)^9 + x(8)*x(3)^9 + x(9)*x(4)^9 + x(10)*x(5)
];
end
clc
clear all;
format long
x0 = [0.9; 0.5; 0.1; -0.5; -0.9; 0.2; 0.4; 0.5; 0.4; 0.2]; %Guess
F0 = fcn(x0);
[x,fval]=fsolve(#fcn, x0) %solve without optimization
options = optimset('MaxFunEvals',10000, 'MaxIter', 10000); %optimization criteria
[x,fval]=fsolve(#fcn, x0, options) %solve with optimization
The error that I get are:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in fcn(line 4) %This is from the function script
F=[x(6) + x(7) + x(8) + x(9) + x(10)
- 2 ;
Error in fcncall (line 7) %This is from the main script
F0 = fcn(x0);
This is a very subtle error I've encountered before. When creating an array literal as you are doing fcn, negative signs that are directly attached to numbers and preceded by a space, as in rows 5, 7, and 9 of your array literal, are viewed as unary operators (i.e., the - makes the number negative and does not act as a binary minus operation). Therefore, because Matlab allows the delimiting of columns to be made with spaces, the indicated rows are interpreted to have two columns; row 5 column 1 is x(6)*x(1)^4 ... x(10)*x(5), and row 5 column 2 is -2/5.
So, either put a space between the three numbers or eliminate all spaces between the minus signs. For example:
x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5) - 2/5;
or
x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5)-2/5;
That problem wasn't easy to find. Not using a blank before the minus at the end of the rows, you created a row with two elements instead of one. For a simplified example compare this:
>> [2 - 2]
ans =
0
>> [2 -2]
ans =
2 -2
>> [(2 -2)]
ans =
0
Now the corrected version of your code:
F=[x(6) + x(7) + x(8) + x(9) + x(10) - 2 ;
x(6)*x(1) + x(7)*x(2) + x(8)*x(3) + x(9)*x(4) + x(10)*x(5) ;
x(6)*x(1)^2 + x(7)*x(2)^2 + x(8)*x(3)^2 + x(9)*x(4)^2 + x(10)*x(5) - 2/3 ; ...either set a space
x(6)*x(1)^3 + x(7)*x(2)^3 + x(8)*x(3)^3 + x(9)*x(4)^3 + x(10)*x(5) ;
(x(6)*x(1)^4 + x(7)*x(2)^4 + x(8)*x(3)^4 + x(9)*x(4)^4 + x(10)*x(5) -2/5); ...or use brackets
x(6)*x(1)^5 + x(7)*x(2)^5 + x(8)*x(3)^5 + x(9)*x(4)^5 + x(10)*x(5) ; ...both fixes the problem
x(6)*x(1)^6 + x(7)*x(2)^6 + x(8)*x(3)^6 + x(9)*x(4)^6 + x(10)*x(5) - 2/7 ;
x(6)*x(1)^7 + x(7)*x(2)^7 + x(8)*x(3)^7 + x(9)*x(4)^7 + x(10)*x(5) ;
x(6)*x(1)^8 + x(7)*x(2)^8 + x(8)*x(3)^8 + x(9)*x(4)^8 + x(10)*x(5) - 2/9 ;
x(6)*x(1)^9 + x(7)*x(2)^9 + x(8)*x(3)^9 + x(9)*x(4)^9 + x(10)*x(5) ];

Implementing a Neural Network in Matlab/Octave

I am trying to solve the problem http://postimg.org/image/4bmfha8m7/
I am having trouble in implementing the weight matrix for the 36 inputs.
I have a 3 neuron hidden layer.
I use the backpropagation algorithm to learn.
What I have tried so far is:
% Sigmoid Function Definition
function [result] = sigmoid(x)
result = 1.0 ./ (1.0 + exp(-x));
end
% Inputs
input = [1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0;
0 0 0 0 1 0 1 1 1 0 0 1 1 1 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 0 0 0 0 0 1;
0 0 0 0 0 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0 0;
0 0 0 0 1 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0 0 0 0 1];
% Desired outputs
output = [1;1;1;1];
% Initializing the bias (Bias or threshold are the same thing, essential for learning, to translate the curve)
% Also, the first column of the weight matrix is the weight of the bias values
bias = [-1 -1 -1 -1];
% Learning coefficient
coeff = 1.0;
% Number of learning iterations
iterations = 100;
disp('No. Of Learning Iterations = ');
disp(iterations);
% Initial weights
weights = ones(36,36);
% Main Algorithm Begins
for i = 1:iterations
out = zeros(4,1);
numIn = length (input(:,1));
for j = 1:numIn
% 1st neuron in the hidden layer
H1 = bias(1,1)*weights(1,1) + input(j,1)*weights(1,2) + input(j,2)*weights(1,3) + input(j,3)*weights(1,4)+ input(j,4)*weights(1,5) + input(j,5)*weights(1,6) + input(j,6)*weights(1,7)
+ input(j,7)*weights(1,8) + input(j,8)*weights(1,9) + input(j,9)*weights(1,10)+ input(j,10)*weights(1,11) + input(j,11)*weights(1,12) + input(j,12)*weights(1,13)
+ input(j,13)*weights(1,14) + input(j,14)*weights(1,15) + input(j,15)*weights(1,16)+ input(j,16)*weights(1,17) + input(j,17)*weights(1,18) + input(j,18)*weights(1,19)
+ input(j,19)*weights(1,20) + input(j,20)*weights(1,21) + input(j,21)*weights(1,22)+ input(j,22)*weights(1,23) + input(j,23)*weights(1,24) + input(j,24)*weights(1,25)
+ input(j,25)*weights(1,26) + input(j,26)*weights(1,27) + input(j,27)*weights(1,28)+ input(j,28)*weights(1,29) + input(j,29)*weights(1,30) + input(j,30)*weights(1,31)
+ input(j,31)*weights(1,32) + input(j,32)*weights(1,33) + input(j,33)*weights(1,34)+ input(j,34)*weights(1,35) + input(j,35)*weights(1,36)
x2(1) = sigmoid(H1);
% 2nd neuron in the hidden layer
H2 = bias(1,2)*weights(2,1) + input(j,1)*weights(2,2) + input(j,2)*weights(2,3) + input(j,3)*weights(2,4)+ input(j,4)*weights(2,5) + input(j,5)*weights(2,6) + input(j,6)*weights(2,7)
+ input(j,7)*weights(2,8) + input(j,8)*weights(2,9) + input(j,9)*weights(2,10)+ input(j,10)*weights(2,11) + input(j,11)*weights(2,12) + input(j,12)*weights(2,13)
+ input(j,13)*weights(2,14) + input(j,14)*weights(2,15) + input(j,15)*weights(2,16)+ input(j,16)*weights(2,17) + input(j,17)*weights(2,18) + input(j,18)*weights(2,19)
+ input(j,19)*weights(2,20) + input(j,20)*weights(2,21) + input(j,21)*weights(2,22)+ input(j,22)*weights(2,23) + input(j,23)*weights(2,24) + input(j,24)*weights(2,25)
+ input(j,25)*weights(2,26) + input(j,26)*weights(2,27) + input(j,27)*weights(2,28)+ input(j,28)*weights(2,29) + input(j,29)*weights(2,30) + input(j,30)*weights(2,31)
+ input(j,31)*weights(2,32) + input(j,32)*weights(2,33) + input(j,33)*weights(2,34)+ input(j,34)*weights(2,35) + input(j,35)*weights(2,36)
x2(2) = sigmoid(H2);
% 3rd neuron in the hidden layer
H3 = bias(1,3)*weights(3,1) + input(j,1)*weights(3,2) + input(j,2)*weights(3,3) + input(j,3)*weights(3,4)+ input(j,4)*weights(3,5) + input(j,5)*weights(3,6) + input(j,6)*weights(3,7)
+ input(j,7)*weights(3,8) + input(j,8)*weights(3,9) + input(j,9)*weights(3,10)+ input(j,10)*weights(3,11) + input(j,11)*weights(3,12) + input(j,12)*weights(3,13)
+ input(j,13)*weights(3,14) + input(j,14)*weights(3,15) + input(j,15)*weights(3,16)+ input(j,16)*weights(3,17) + input(j,17)*weights(3,18) + input(j,18)*weights(3,19)
+ input(j,19)*weights(3,20) + input(j,20)*weights(3,21) + input(j,21)*weights(3,22)+ input(j,22)*weights(3,23) + input(j,23)*weights(3,24) + input(j,24)*weights(3,25)
+ input(j,25)*weights(3,26) + input(j,26)*weights(3,27) + input(j,27)*weights(3,28)+ input(j,28)*weights(3,29) + input(j,29)*weights(3,30) + input(j,30)*weights(3,31)
+ input(j,31)*weights(3,32) + input(j,32)*weights(3,33) + input(j,33)*weights(3,34)+ input(j,34)*weights(3,35) + input(j,35)*weights(3,36)
x2(3) = sigmoid(H3);
% Output layer
x3_1 = bias(1,4)*weights(4,1) + x2(1)*weights(4,2) + x2(2)*weights(4,3) + x2(3)*weights(4,4);
out(j) = sigmoid(x3_1);
% Adjust delta values of weights
% For output layer: delta(wi) = xi*delta,
% delta = (1-actual output)*(desired output - actual output)
delta3_1 = out(j)*(1-out(j))*(output(j)-out(j));
% Propagate the delta backwards into hidden layers
delta2_1 = x2(1)*(1-x2(1))*weights(3,2)*delta3_1;
delta2_2 = x2(2)*(1-x2(2))*weights(3,3)*delta3_1;
delta2_3 = x2(3)*(1-x2(3))*weights(3,4)*delta3_1;
% Add weight changes to original weights and then use the new weights.
% delta weight = coeff*x*delta
for k = 1:4
if k == 1 % Bias cases
weights(1,k) = weights(1,k) + coeff*bias(1,1)*delta2_1;
weights(2,k) = weights(2,k) + coeff*bias(1,2)*delta2_2;
weights(3,k) = weights(3,k) + coeff*bias(1,3)*delta2_3;
weights(4,k) = weights(4,k) + coeff*bias(1,4)*delta3_1;
else % When k=2 or 3 input cases to neurons
weights(1,k) = weights(1,k) + coeff*input(j,1)*delta2_1;
weights(2,k) = weights(2,k) + coeff*input(j,2)*delta2_2;
weights(3,k) = weights(3,k) + coeff*input(j,3)*delta2_3;
weights(4,k) = weights(4,k) + coeff*x2(k-1)*delta3_1;
end
end
end
end
disp('For the Input');
disp(input);
disp('Output Is');
disp(out);
disp('Test Case: For the Input');
input = [1 1 0 1 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0];
For me the problem is the labeling, I can't see where do you have the output
Output (1,1,1,1)? What do you mean. Perhaps I miss something but for me there are two ways of labeling a multiclass clasification one is with a label directly (0 for A, 1 for B, 3 for C...) and expanding it after or directly expanded like A=1,0,0,0 = [1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1]
The way that you make operations is very easy to make a mistakes, take a look to matlab/octave matrix operations, it's very powerful and could simplify everything a lot.

Some Boolean Algebra Simplication basic

I wanna ask some basic law of boolean algebra.
What i learn is :
1. A+A'B=A+B
2. A+AB'=A+B'
3. A+AB=A
4. A+A'B'=A+B'
but i meet some condition like :
A'+AB
so, what is the answer for A'+AB?
Let's say A' = D so when A is false, then D is true and vice versa.
Then A' + AB = D + D'B and if you understand your first equation:
D + D'B = D + B = A' + B
Regarding your comment:
I'll use this equality: AB + A'B = B and I will combine the first with the third and the second with the fifth term:
x'y'z'+x'yz+xy'z'+xy'z+xyz = y'z' + yz + xy'z
Now, from the result, I can do this:
y'z' + yz + xy'z = yz + y'(z' + zx)
and now, using using A' + AB = A' + B:
yz + y'(z' + zx) = yz + y'(z' + x) = yz + y'z' + y'x
or do this:
y'z' + yz + xy'z = y'z' + z(y+ xy') = y'z' + z(y + x) = y'z' + zy + xz
Are they different? No, take a look at this:
x y z | yz + y'z' + y'x | y'z' + zy + xz
0 0 0 | 1 | 1
0 0 1 | 0 | 0
0 1 0 | 0 | 0
0 1 1 | 1 | 1
1 0 0 | 1 | 1
1 0 1 | 1 | 1
1 1 0 | 0 | 0
1 1 1 | 1 | 1
You can use this open source project to solve basic boolean expression, its solve all the basic boolean expression

How do I average elements of a matrix according to an identity matrix?

I have a 333x1 vector of values ('data') and each of the cells in the vector correspond to a range of 1 of 13 subcategories. The identities of each of these subcategories are stored in a separate identity matrix ('id'). I'd like to calculate the sum of the values within the original data matrix that have a similar identity.
e.g. pretending for this example that 'data' and 'id' are 8x1 vectors
data = [1;1;1;0;0;0;1;1]
id = [1;2;1;2;3;3;1;3]
sum of id 1: 1 + 0 + 1 + 0 + 0 + 0 + 1 + 0 = 3
sum of id 2: 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 = 1
sum of id 3: 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 = 1
I'm sure that there is a really easy fix for this, however I can't seem to work it out.
Thanks for your time
Mac
A simple solution would be:
numCategories = 13;
totals = zeros(numCategories,1);
for idnum = 1:numCategories
totals(idnum) = sum((id==idnum).*data);
end
EDIT: As knedlsepp pointed out in the comments, the accumarray function accomplishes exactly what the above code does in one line.
accumarray(id,data);