Hello consider following code:
Z = cache
dZ = np.array(dA, copy=True) # just converting dz to a correct object.
dZ[Z <= 0] = 0
I succeed for debugging reasons to print z and dz with following code
print("z = " + str(cache))
print("dz np array = " + str(np.array(dA, copy=True)))
But now my question. I like also to print the content of dz after the expression dZ[Z <= 0] = 0 but I do not succeed without any errors. What is the code I have to write to print the content of dZ after the indexing with boolean arrays
tnx,
Related
I'm facing an issue with this simple Fibonacci number generator program:
function f = fibonr(n)
f(1) = 1;
f(2) = 1;
for i = 3:n
f(i) = f(i-1) + f(i-2);
end
end
If I want to display only the n-th number of the sequence, what adjustments should I make?
function f = fibonr(n)
f = zeros(n,1); %//initialise the output array
f(1,1) = 1;
f(2,1) = 1;
for ii = 3:n
f(ii,1) = f(ii-1,1) + f(ii-2,1);
end
%//create a string with text and variables
str = sprintf('The %d th number in the Fibonacci sequence is %d',n,f(ii,1));
disp(str) %//display your output.
end
first up: don't use i as a variable. Secondly, I switched to using column vectors, since MATLAB processes them faster, as well as I initialised the array, which is way faster (hence the shiny orange wiggles below your f(i)= line).
Call your function:
output = fibonr(10);
The 10 th number in the Fibonacci sequence is 55
If you use e.g. n=20 and still want the 10th argument just call output(10)
if you want the specified output right away you can use nargin. This code will give you all of the sequence if you call fibonr(n), or you can specify a vector to get the fibonacy numbers at said positions. If you are interested in both, your specified output and all the numbers, you can call the function with:
[output, fibnumbers] = fibonr(n,v);
function [output,f] = fibonr(n,v)
f(1) = 1;
f(2) = 1;
for i = 3:n
f(i) = f(i-1) + f(i-2);
end
if nargin() > 1
output = f(v);
else
output = f;
end
I need to write a scalar function that gets a vector with unknown length. I have made an example:
%gets vector and returns x1^1 + x2^2 + ... + xn^n
function [ output ] = func2( x )
u=0;
for j = 1 : length(x)
u = u + x(j)^j;
end
output = u;
end
and
%gets vector and returns sin(x1) + sin(x2) + ... + sin(xn)
function func = func1( x )
func = sum(sin(x));
end
but when I try to calculate gradient of this function I get 0 for example:
func1([1,2,3]) = -0.865837027279448
but
gradient(func1([1,2,3]),[1,2,3]) = 0
I am looking for a way to implement gradient(func1,x) and get cos(x(1)) cos(x(2)) ... (cos(x(n)) so in the example above it suppose to be:
gradient(func1([1,2,3]),[1,2,3]) = [cos(1),cos(2),cos(3)]
Can anyone suggest the best way to approach this problem?
In matlab usually we add a header using fprintf command.
This is a problem when the size of the table depends on the input and when it exceeds a certain range (more than total number of column able to be presented in the command window).
When this occurs the header which we specified previously using the fprintf command will not be compatible with the current output data.
I would like to know is there a way like adding a character string into the 1st row of the matrix during some kind of iteration process. I had tried hardly but still can't find a proper way to solve this issue.
Or it is actually cannot be done in matlab for this purpose.
Eg
clear;clc
A = [2 8 3 1;0 2 -1 4;7 -2 1 2;-1 0 5 2]
B = [-2;4;3;5]
Es = 1e-5
n = length(B);
x = zeros(n,1);
Ea = ones(n,1);
iter = 0;
while max(Ea) >= Es
if iter <= 30
iter = iter + 1;
x_old = x;
for i = 1:n
j = 1:n;
j(i) = [];
x_cal = x;
x_cal(i) = [];
x(i) = (B(i) - sum(A(i,j) * x_cal)) / A(i,i);
end
else
break
end
x_ans(:,iter) = x;
Ea(:,iter) =abs(( x - x_old) ./ x);
end
result = [1:iter; x_ans; Ea]'
for the coding above..how could I add the heading like iteration for 1st column, x1...x2...x3..xn for nth column and error x1..error x2..error xn for another n column. I would like to make this heading can be automated generated based on the input matrix
If the size of the table depends on the input, use a cell array, using c = cell(...).
In each iteration simply call c{i,j} instead of c[i,j].
load X_Q2.data
load T_Q2.data
x = X_Q2(:,1);
y = X_Q2(:,2);
learningrate = 0.2;
max_iteration = 50;
% initialize parameters
count = length(x);
weights = rand(1,3); % creates a 1-by-3 array with random weights
globalerror = 0;
iter = 0;
while globalerror ~= 0 && iter <= max_iteration
iter = iter + 1;
globalerror = 0;
for p = 1:count
output = calculateOutput(weights,x(p),y(p));
localerror = T_Q2(p) - output
weights(1)= weights(1) + learningrate *localerror*x(p);
weights(2)= weights(1) + learningrate *localerror*y(p);
weights(3)= weights(1) + learningrate *localerror;
globalerror = globalerror + (localerror*localerror);
end
end
I out this fuunc in some other file.
function result = calculateOutput (weights, x, y)
s = x * weights(1) + y * weights(2) + weights(3);
if s >= 0
result = 1;
else
result = -1;
end
Nothing is coming out. I out the code in the command window and press enter....nothing apperars on the window. How do i get the output?
You can't use the variable globalerror in the condition check of your while loop because you don't define that variable as anything until within the loop. This is why you are getting the error "Undefined function or variable 'globalerror'". You have to initialize globalerror to some value before you try to use it in any statements.
Also, as I mentioned in my answer to your previous question, you can't declare functions inside scripts. Try cutting out the function calculateOutput from the above script and place it in its own file called calculateOutput.m, then save that somewhere on the MATLAB path.
And a few additional problems I see:
MATLAB uses 1-based indexing, not 0-based indexing. In other words, the first element of a vector or a matrix dimension is indexed by the value 1, not 0.
I have no idea what you are trying to do with this line:
localerror = output(p) - output
since the variable output is a scalar in your code, not a vector that can be indexed by p.
I keep getting this error in Matlab:
Attempted to access r(0,0); index must be a positive integer or
logical.
Error in ==> Romberg at 15
I ran it with Romberg(1.3, 2.19,8)
I think the problem is the statement is not logical because I made it positive and still got the same error. Anyone got some ideas of what i could do?
function Romberg(a, b, n)
h = b - a;
r = zeros(n,n);
for i = 1:n
h = h/2;
sum1 = 0;
for k = 1:2:2^(i)
sum1 = sum1 + f(a + k*h);
end
r(i,0) = (1/2)*r(i-1,0) + (sum1)*h;
for j = 1:i
r(i,j) = r(i,j-1) + (r(i,j-1) - r(i-1,j-1))/((4^j) - 1);
end
end
disp(r);
end
function f_of_x = f(x)
f_of_x = sin(x)/x;
end
There are two lines where you're using 0 to index, which you can't in Matlab:
r(i,0) = (1/2)*r(i-1,0) + (sum1)*h;
and
r(i,j) = r(i,j-1) + (r(i,j-1) - r(i-1,j-1))/((4^j) - 1);
when j==1 or i==1.
I suggest that you run your loops starting from 2, and replace the exponents i and j with (i-1) and (j-1), respectively.
As an aside: You could write the loop
for k = 1:2:2^(i)
sum1 = sum1 + f(a + k*h);
end
as
k = 1:2:2^i;
tmp = f(a + k*h);
sum1 = sum(tmp);
if you write f_of_x as
sin(x)./x
In MATLAB, vectors and matrices are indexed starting from 1. Therefore, the very first line of your code is invalid because the index on r is 0.
You have zero subscripts in
r(i,0) = (1/2)*r(i-1,0) + (sum1)*h;
This is impossible in MATLAB -- all indices start form 1.