How would I use recursion to create an array of extra change($100, 50, 20, 10, 5, 1) MATLAB - matlab

How would I correctly make a recursive call within every if-statement to get the change of money? Im specifically focusing on the "change" variable.Thanks
TEST CASE 1-------------------------------------------------------------------------------
<>> [change,flag] = makeChangeRecursive(2,100)
change =
50
20
20
5
2
1
flag =
1
My code is the following
function [change,flag] = makeChangeRecursive(cost,paid)
if extra > 0
flag = true;
elseif extra == 0
change = 0;
flag = true;
return
elseif cost > paid;
flag = false;
change = [];
warning('That''s not enough to buy that item.');
return
end
if extra >= 100
change = [change; makeChangeRecursive(cost,paid - change )];
paid =paid-100;
elseif extra >= 50
change = [change; 50];
paid =paid-50;
elseif
This continues for all dollar values.

Let's take a look at your first case:
if extra >= 100
change = [change; makeChangeRecursive(cost,paid - change )];
paid =paid-100;
elseif ...
The first time we call your function, the variable change doesn't have anything in it. In fact, it will never have anything in it at the beginning of the function call because you don't pass it in as a parameter or give it a value prior to this line. So putting change on the right-hand side of the assignment will give you an error.
But that's okay, because that's not what you want to do anyway. You want to build change up from the beginning.
In addition, change is a list of values. We want to pass the recursive calls a single value, paid after updating its value.
Let's build this up step by step:
if extra >= 100
If this is true, we want subtract 100 from the amount paid (what we pass in to the recursive call) and add 100 to our list of change. Let's do the first part:
paid = paid - 100;
As I said, we want to update paid first because we're going to use this value in the recursive call, which happens next, along with adding our new change value to the list:
change = [100; makeChangeRecursive(cost, paid)];
elseif ...
And so on for the remainder of the change values. I'm sure you can take care of the rest of them now by yourself.
I also noticed that you didn't assign a value to extra. This might have been just a cut-and-paste error, but you need to make sure that you have that at the beginning of your function.

Related

Libreoffice calc - how to write a same value into a range

I know how to 'select' a range in LO (7.2.4.1) Calc BASIC ....
ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
But how to write a value, e.g. "1", into that range using BASIC?
myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
myRange.Value = 1
Gives an "property or method not found" error. But I can't find any properties or values to go after Range to allow me to do what I want. Flailing around and trying
myRange.setValue = 1
myRange.writeValue = 1
myRange.setString = "1"
and numerous other variants don't work either.
Would really appreciate the solution. Thanks.
You can edit the value of an individual cell, but not the entire range. You will have to iterate over all the cells in the range one at a time, changing the value of each of them.
Sub Set1ToD1H6
myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
For i = 0 To myRange.getRows().getCount()-1
For j = 0 To myRange.getColumns().getCount()-1
myRange.getCellByPosition(j, i).setValue(1)
Next j
Next i
End Sub
But since the read-write operation to a cell is comparable in time to the read-write operation to a whole range, it is preferable to use another method - to prepare data in an array and write from it to a range in one operation:
Sub Set1ToRange
myRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByName("D1:H6")
dataOfRange = myRange.getData()
For i = LBound(dataOfRange) To UBound(dataOfRange)
For j = LBound(dataOfRange(i)) To UBound(dataOfRange(i))
dataOfRange(i)(j) = 1
Next j
Next i
myRange.setData(dataOfRange)
End Sub
(For your example, this will be approximately 30 times faster, for a larger range the time winnings will be even more significant)
The .getData() and .setData() methods work on numeric range values. To work with text strings (and numbers), use .getDataArray() and .setDataArray(), for working with cell formulas use .getFormulaArray() and .setFormulaArray()

Mean of values before and after a specific element

I have an array of 1 x 400, where all element values are above 1500. However, I have some elements that have values<50 which are wrong measures and I would like to have the mean of the elements before and after the wrong measured data points and replace it in the main array.
For instance, element number 17 is below 50 so I want to take the mean of elements 16 and 18 and replace element 17 with the new mean.
Can someone help me, please? many thanks in advance.
No language is specified in the question, but for Python you could work with List Comprehension:
# array with 400 values, some of which are incorrect
arr = [...]
arr = [arr[i] if arr[i] >= 50 else (arr[i-1]+arr[i+1])/2 for i in range(len(arr))]
That is, if arr[i] is less than 50, it'll be replaced by the average value of the element before and after it. There are two issues with this approach.
If i is the first or last element, then one of the two values will be undefined, and no mean can be obtained. This can be fixed by just using the value of the available neighbour, as specified below
If two values in a row are very low, the leftmost one will use the rightmost one to calculate its value, which will result in a very low value. This is a problem that may not occur for you in practice, but it is an inherent result of the way you wish to recalculate values, and you might want to keep it in mind.
Improved version, keeping in mind the edge cases:
# don't alter the first and last item, even if they're low
arr = [arr[i] if arr[i] >= 50 or i == 0 or i+1 == len(arr) else (arr[i-1]+arr[i+1])/2 for i in range(len(arr))]
# replace the first and last element if needed
if arr[0] < 50:
arr[0] = arr[1]
if arr[len(arr)-1] < 50:
arr[len(arr)-1] = arr[len(arr)-2]
I hope this answer was useful for you, even if you intend to use another language or framework than python.

How to contunally add to a number in SparkAR ( += equivalent)

New to reactive programming and pretty lost.
I have a number (which can be positive or negative) coming into a script from a patch in SparkAR and I'd like to add the number to itself once every frame.
ie if the incoming number is 1 and it comes in 9 times the variable would be 9.
let intoScript = Patches.getScalarValue('intoScript').pinLastValue;
let myValue = Reactive.add(myValue, intoScript);
The above doesnt work.
One way-
Increment counter in Patch editor and send variable value to SparkAR.
(https://sparkar.facebook.com/ar-studio/learn/documentation/docs/visual-programming/javascript-to-patch-bridging/)
let originalValue = Reactive.val(0)
function increment(newValue){
Diagnostics.watch('originalValue', originalValue)
originalValue = newValue.add(originalValue)
}
Don't forget to import Reactive module. more info about logical operations here

AnyLogic change parameter value at a process

In AnyLogic Simulation software, I have an agent that has some parameters. I just want that whenever this agent passes from a specific process, say delay, its parameter value to be changed to another value. Moreover, if I have 100 individuals (all same type of agents) passing from this process, I will change the first 40 of these individuals' parameter values to 1, and the last 60's values to 2. Is this possible? Do I need to write codes to these boxes:
Yes, you have to write some code on one of these boxes.
Create an integer variable named v_countand set the initial value to 0. If you want agent's parameters to be changed as soon as they enter the block write this code "On enter" box, else if you want them to be changed when they leave the block write it "On at exit":
if(v_count < 40)
agent.parameter = value1;
else
agent.parameter = value2;
v_count++;
//reset the count to 0 when 100 agents have passed through this block
if(v_count == 100)
v_count = 0;

Specman coverage: Is there a way to define ranges using variable?

I have comp_value that gets values between 1 .. 100.
In addition I have an input variable period (of the same range). I need to cover 2 ranges of comp_values: [1..period] and [period+1 .. 100]. Something like this:
cover some_event_e is {
item period using no_collect;
item comp_val using no_collect,
ranges = {
range([1..period], "Smaller_than_period");
range([period+1..100], "Bigger_than_period");
};
};
(The code causes compilation error since no variable can be written inside range).
Is there a way to collect the coverage?
Thank you for your help.
Ranges must be constant.
But if I understood your intent correctly, you can define new items like
cover some_event_e is {
item smaller_or_equal_than_period: bool = (comp_val in [1..period]) using
ignore = (not smaller_or_equal_than_period);
item greater_than_period: bool = (comp_val in [(min(100,period+1)..100]) using
ignore = (not greater_than_period);
};
Assuming period is always in [1..100].