This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 8 years ago.
I am trying to code a simple backpropagation network in Matlab, and I am getting the following error:
Subscript indices must either be real positive integers
or logicals.
in line 144 of my code, which is during this section:
for l = 1:net.layerCount,
if l == 1, % From input layer
for i = round(1:net.inputSize),
i % i = 1 and
l % l = 1 when I get the error.
% error in this next line:
net.weight{l}(i,:) = net.weight{l}(i,:) ...
- sum(lrate .* net.delta{l} .* net.layerOutput{l-1}(i)) ...
- (momentum .* net.previousWeightDelta{l}(i,:));
net.previousWeightDelta{l}(i,:) = net.weight{l}(i,:);
end
else
for i = 1:net.layerSize{l-1},
net.weight{l}(i,:) = net.weight{l}(i,:) ...
- sum(lrate .* net.delta{l} .* net.layerOutput{l-1}(i)) ...
- (momentum .* net.previousWeightDelta{l}(i,:));
net.previousWeightDelta{l}(i,:) = net.weight{l}(i,:);
end
end
end
The error persists even if I surround 1:net.layerCount and the other loop vectors with round(). Any idea why this is the case?
Thanks!
In the l == 1 case, you illegally try to use
net.layerOutput{l-1}
0 is not positive.
The input layer needs to use the inputs, not inter-layer connections.
Related
This question already has an answer here:
Subscript indices must either be real positive integers or logical error
(1 answer)
Closed 10 months ago.
I'm new to matlab and trying to express the function:
f(z)=(1-exp((1+v)./z)).*(z./y).*cos(z)
with the values: >> v=3.4 >> y=6.9 >> z=8:1.21:328.65
but always get the error message "Array indices must be positive integers or logical values."
Please help
If you are writing f(z) into matlab it means z index of array f. You need to say r = (1-exp((1+v)./z)).*(z./y).*cos(z):
v = 3.4;
y = 6.9;
z=8:1.21:328.65;
f = (1-exp((1+v)./z)).*(z./y).*cos(z);
This question already has answers here:
Why is 24.0000 not equal to 24.0000 in MATLAB?
(6 answers)
Closed 3 years ago.
I got a problem about checking if two matrices are equal in MATLAB.
Specifically, I want to verify if, for a matrix W, all elements are equal to each other and all row sums are equal to 1 we would have W = W^2.
Therefore I wrote the following code which aims to check if every element of these two matrices are equivalent, to conclude whether the matrices are equal to each other. But it turns out that this does not work since the W8 matrix should be equal to its square.
for i = 1 :60
for j = 1 :60
if(W8(i,j) - W8_square(i,j) ~= 0)
disp('the matrix are not equal');
break;
end
end
end
There is a matlab function for it:
eq = isequal(W8,W8_square) should work
Here you find the reference
https://www.mathworks.com/help/matlab/ref/isequal.html
Be careful that if this checks for EXACT identity, so computation errors, of the order of eps, may affect the result.
To solve this, I would subtract the two matrixes and check the norm of the result: below a certain threshold (low) they are equal.
Here you have an example code for your problem:
n = 10; %matrix size
W8 = ones(n)/n; %Generating W8
W8_square = W8^2;
eq = isequal(W8,W8_square) %checking EXACT identity
M_difference = W8-W8_square; %Difference matrix
eq2 = isequal(M_difference<=eps,ones(n)) %%comparing every value with eps
This question already has answers here:
Using transpose versus ctranspose in MATLAB
(4 answers)
Closed 5 years ago.
In matlab I have a matrix As
As = zeros(m, n);
Next I assign values to As and transpose the specific columns:
for idx = 1:n
% Assign value to As, then assign to 'a' and 's'
a = As(:, idx)';
s = As(:, idx);
end
Then s is a column vector like:
s = [0.1 - 0.2i
0.3 + 0.4i]
But elements in a have the flipped signs:
a = [0.1 + 0.2i, 0.3 - 0.4i]
This is confusing me, I mean the transpose of s should be a row (no problem) with the symbols in the order -, + like
a = [0.1 - 0.2i, 0.3 + 0.4i]
Can anyone tell me what the problem is?
The prime operator ' in matlab is actually an alias to ctranspose, which does not only convert rows to columns, or columns to rows of ordinary matrices or vectors, but also calculates the complex conjugate, i.e. changes the sign of the imaginary part.
The non-conjugate transpose operator A.', performs a transpose without conjugation. That is, it doesn't change the imaginary parts of the elements.
This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 8 years ago.
I am new to matlab and matlab gives error 'Subscript indices must either be real positive integers or logicals'at the last line of the code segment ( if th<0.01 || rank(A'*A)~=2) shown below, please guide me for this:
function [u,v] = optical_flow( im1,im2,windowSize )
x_c=im1(1:end-1,2:end)-im2(1:end-1,1:end-1);%(define rows 1~479,2~639 columns)-(rows 1~479,1~639)
y_c=im2(2:end,1:end-1)-im1(1:end-1,1:end-1);
t_c=im2(1:end-1,1:end-1)-im1(1:end-1,1:end-1);
%initialize for speed
u = zeros(size(x_c));
v = u;
for x = 1:size(x_c,1)-windowSize %fpr all x
for y = 1:size(x_c,2)-windowSize
%Get the windows of the dimensions
win_x=imcrop(x_c,[x y windowSize windowSize]);
win_y=imcrop(y_c,[x y windowSize windowSize]);
win_t=imcrop(t_c,[x y windowSize windowSize]);
%Convert windows to vectors to produce A for solving later
A = [win_x(:) win_y(:)];
%Compute threshold t (smallest eigenvalue of A'A)
th=min(eig(A'*A));
%Optical flow is only valid in regions with t<0.01 and
rank =2;
%if true, then it should not be computed
if th<0.01 || rank(A'*A)~=2
You assign a value to rank, which means that MATLAB now treats rank as a variable. You later treat rank as a function when you try to calculate rank(A'*A). You should rename your rank variable.
%Optical flow is only valid in regions with t<0.01 and
rank =2; % <--- RENAME THIS
%if true, then it should not be computed
This question already has answers here:
Is there a Matlab conditional IF operator that can be placed INLINE like VBA's IIF
(10 answers)
Closed 9 years ago.
I'm looking for Matlab equivalent of c# condition ? true-expression : false-expression conditional assignment. The most I know of is a = 5>2, which is true\false assignment,
but is there any one line conditional assignment for
if condition a=1;else a=2; end?
For numeric arrays, there is another solution --
// C:
A = COND ? X : Y;
becomes
% MATLAB
% A, X and Y are numerics
% COND is a logical condition.
A = COND.*X + (~COND).*Y ;
Advantage:
works wonderfully in parallel for vectors or large arrays - each item in A gets assigned depending on the corresponding condition. The same line works for:
condition is scalar, arrays X and Y are equal in size
condition is an array of any size, X and Y are scalars
condition and X and Y are all arrays of the same size
Warning:
Doesn't work gracefully with NaNs. Beware! If an element of X is nan, or an element of Y is nan, then you'll get a NaN in A, irrespective of the condition.
Really Useful corollary:
you can use bsxfun where COND and X/Y have different sizes.
A = bsxfun( #times, COND', X ) + bsxfun( #times, ~COND', Y );
works for example where COND and X/Y are vectors of different lengths.
neat eh?
One line conditional assignment:
a(a > 5) = 2;
This is an example of logical indexing, a > 5 is a logical (i.e. Boolean or binary) matrix/array the same size as a with a 1 where ever the expression was true. The left side of the above assignment refers to all the positions in a where a>5 has a 1.
b = a > 5; % if a = [9,3,5,6], b = [1,0,0,1]
a(~b) = 3;
c = a > 10;
a(b&c) = ...
Etc...you can do pretty much anything you'd expect with such logical arrays.
Matlab does not have a ternary operator. You though easily write a function that will do such thing for you:
function c = conditional(condition , a , b)
if condition
c = a;
else
c = b;
end
end