I would just like to know why this returns an "Expected end of statement" whenever I call the function multiply_matrix(matrixA, matrixB) and feed it with matrixA (3x3 matrix), and matrixB(3x3 matrix).
The error is always at "Next k".
This is the code of the function.
Function multiply_matrix(matrixA, matrixB)
dim answer_matrix(3,3)
for i=0 to UBound(matrixA,1)
for j=0 to UBound(matrixB,2)
sum = 0
for k=0 to UBound(matrixB,1)
sum = sum + ( matrixA(i,k) * matrixB(k,j) )
next k
answer_matrix(i,j) = sum
next j
next i
multiply_matrix = answer_matrix
End Function
Other Basic dialects allow variable names after the Next, VBScript doesn't.
Related
I want to get a cummulative relative Performance Line.
I get this error message. I don't know what i am doing wrong. Im trying it now for some time now. Can you help me?
Error: "Mismatched input '|PE|' expecting 'end of line without line continuation'."
change = (close[1]-close[2])/close[2])
n = 252
sum = 0
sais(change, n) => for i=0 to n-1
sum := sum + change [n]
plot(sais, color=color.blue)
The weird error is most likely caused by the fact that you use both the single-line and the multi-line function declaration syntax. It has to be multiline because of the for cycle, so the first line after => should be empty, and the code should start on the next line, indented. This is how your function should look (formatting-wise):
sais(change, n) =>
for i=0 to n-1
sum := sum + change [n]
Note that this won't work either because the function cannot modify a global variable sum. You'd need to create a local variable inside the scope of the function to store the value and then return it and assign it to your global sum. Depending on what you want to achieve, it might look something like this:
<...>
sum = 0
sais(change, n) =>
local_sum = 0
for i=0 to n-1
local_sum := local_sum + change[n]
local_sum
sum := sais(change, n)
Hi I am having problems with the following function in Matlab. Can some please help?
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. Write a function called smallest_multiple that returns a uint64, the smallest positive number that is evenly divisible by all of the numbers from 1 to n where n is a positive integer scalar and is the only input argument of the function. If the result would be greater than what can be represented as a uint64, the function returns 0. (Inspired by Project Euler.)
Below is the code I wrote for the function but it gives error
Feedback: Your function made an error for argument(s) 2
Your solution is _not_ correct.
Help please...
function [answer]=smallest_multiple(n)
limit = 1e10;
N = 20;
for i = N:N:limit
for j = N:-1:1
if mod(i,j) ~= 0
break
end
end
if j == 1
answer = i;
break
end
end
fprintf('The smallest evenly divisible number is %.0d\n',answer)
Your function looks correct. However the argument you pass is a lowercase n instead of an uppercase N, which you use during your code.
So the correct function (with limit as argument) is
function [answer]=smallest_multiple(N,limit)
for i = N:N:limit
for j = N:-1:1
if mod(i,j) ~= 0
break
end
end
if j == 1
answer = i;
break
end
end
fprintf('The smallest evenly divisible number is %.0d\n',answer)
I'm attempting to implement Neville's algorithm in MatLab with four given points in this case. However, I'm a little stuck at the moment.
This is my script so far:
% Neville's Method
% Function parameters
x = [7,14,21,28];
fx = [58,50,54,53];
t = 10;
n = length(x);
Q = zeros(n,n);
for i = 1:n
Q(i,1) = fx(i);
end
for j = 2:n
for i = j:n
Q(i,j) = ((t-x(i-j)) * Q(i,j-1)/(x(i)-x(i-j))) + ((x(i)-t) * Q(i-1,j-1)/(x(i)-x(i-j)));
end
end
print(Q);
As for the problem I'm having, I'm getting this output consistently:
Subscript indices must either be real positive integers or logicals.
I've been trying to tweak the loop iterations but to no avail. I know the problem is the primary logic line in the inner loop. Some of the operations result in array indices that are equal to zero initially.
That's where I am, any help would be appreciated!
In your loop at the first time i-j is 0 because you set i = j. In MATLAB indices start at 1. A simple fix to get running code would be to change
for i = j:n
to
for i = j+1:n
This solves
Subscript indices must either be real positive integers or logicals.
However, this may not be ideal and you may need to rethink your logic. The output I get is
>> neville
Q =
58.0000 0 0 0
50.0000 0 0 0
54.0000 50.8571 0 0
53.0000 54.2857 51.3469 0
I'm using fortran 90, and I hope to count the number of occurence, when two numbers appears in an array.
flag=0
q=0
do k=1,ncolumns
if (conn(m,k)==i .and. conn(m,k)==l) flag=1
enddo
if (flag==1) q=q+1
write (*,*) q
Here, conn(m,k) is the matrix, made up of m lines and k columns. I want to read the conn(m,k), and count the number of occurrence when both number i and l are included in conn(m,k). I know above code will not work because it prints out only 0, since that if loop have a problem. But I cannot use '.or.' because I want the count when i and l both are included in the conn(m,k). How can I check both number i and l are included in conn?
I modified the code above like
ncolumns=2
flag=0
q=0
do k=1,ncolumns
!!!if (conn(m,k)==i .and. conn(m,k)==l) flag=1
if (((conn(m,1)==i).and.(conn(m,2)==l)).or.((conn(m,1)==l).and.(conn(m,2)==i))) flag=1
enddo
if (flag==1) q=q+1
write (*,*) q
This works fine, but as you can see, this code is ridiculous since I need to manually define k, specially when 'ncolumns' is huge number. How can I do this with index?
Likewise, how can I check 2 or more specific numbers are included in the matrix like conn(m,k) in fortran? Thanks.
Something like this ought to do what you want:
nums = [2,12,-4,99] ! an array of the numbers you're looking for
q = 0 ! the count of rows containing all the numbers in nums
DO ix = 1, SIZE(conn,1) ! the number of rows in conn
nmatches = 0 ! the number of elements of nums found in conn(ix,:)
DO jx = 1, SIZE(nums)
IF(ANY(conn(ix,:)==nums(jx))) nmatches = nmatches+1 ! figure this out yourself
END DO
! if there are as many matches in this row as there are elements in nums, add 1 to q
IF(nmatches==SIZE(nums)) q = q+1
END DO
You can also use a dummy matrix (dummy_mat) to populate with ones where the values are located in the matrix you are searching (value_mat) and then sum the dummy matrix to get the count (num_entries):
nums = [2,12,-4,99]
do i=1,size(nums) ! loop over the values you are looking for
dummy_mat = 0 ! zero out dummy matrix that is the same size as your value matrix
where (value_mat(:,:) == nums(i))
dummy_mat(:,:) = 1
end where
num_entries(i) = SUM(dummy_mat)
end do
From the comment "if there are 3 lines in conn which have two elements (such as 3 and 12) together, the printed q should be 3".
You can do this with a single loop if you have Fortran95 (I forget if it is in the 90 spec) or later.
Here is an example:
Program Test_Count
Implicit None
Real(Kind=8), Dimension(3) :: nums = (/-2.0_8 , -3.0_8 , -4.0_8/)
Real(Kind=8), Dimension(4,4) :: test
Logical, Dimension(4) :: Mask
Integer :: i,j,NumberOfLines
! Fill test
Do i = 1,4
Do j = 1,4
test(i,j) = -4.0_8 + (j -1)
End Do
End Do
! Count the row that have all the nums in them
Mask = any(test == nums(1),2)
Do i = 2,3
Mask = Mask .and. any(test == num2(i),2)
End Do
NumberOfLines = count(Mask)
Write(*,*) NumberOfLines ! Prints out 4.
End Program Test_Count
ok I am lost right now by this assignment and just need some help.
The assignment is Design a program that generates the sum of numbers.
Given a number (user input) you need an application that will produce a sum of the numbers from 1 to that given number I just need some help to start because I am just having to hard of a time and i know it might seem easy but never had any experience to any of this at all.
var input = getUserInput;
var sum;
while (input > 0)
{
sum = sum + input--;
}
print sum;
You can start with something as straightforward as this:
input = getuserInput()
count = 0
sum = 0
while count < input:
count = count + 1
sum = sum + count
return sum
...then enhance it.
INPUT number
VARIABLE sum = 0
FOR VARIABLE n = 1 TO number WITH STEP 1 DO
sum += n
END FOR
PRINT sum
Translated to lua it would look like this:
number = tonumber( io.read() )
sum = 0
for n = 1, number, 1 do
sum = sum + n
end
print(sum)
Translated into python it would look like
Number = int(input("Number:"))
Sum = 0
for n in range(1,Number+1):
Sum += n
print(Sum)
Though the pythonic way would resemble:
number = int(input("Number:"))
print(sum(range(number+1)))
When applying this to any language look out for the following:
Converting the user's input to an integer, by default it will normally be a string i.e "...".
Declare a variable to hold the total (in our case sum) before you try to add a number to it i.e n.
Make sure your for loop goes from 1 to number