In PySpark, what's the exact value of lambda x,y in the processing of `rdd.reduce(lambda x,y:x+y)`? - pyspark

For example,
rdd=sc.parallelize(range(5),5)
rdd.reduce(lambda x,y:x+y)
Then what's the exact value of lambda x,y in the processing?
I assume that we can just process one value when there is an array as an input. But lambda get two values.
Sorry I can't print it as usual in python.

Related

Apply calculation to each element simultaneously in an array in matlab using a function (Version 2013b)

I have a matrix (type:double) of size 106 x 103. The matrix represents European gridded temperature data for one timestep (a day).
For every day, I want to calculate the degree days (thermal time) for a species, based on the temperature recorded for each 'cell' (i,j element) in the matrix using a formula that I have coded in Matlab based on a sinewave approach.
So, ultimately, what I want to do is being able to apply a calculation to my matrix, that will provide individual output for each grid cell (i,j element) dependent on the temperature data that is recorded there.
I could do this with a loop, but I have to accumulate these degree days for multiple years, so I would prefer to find a way of applying the calculation to each element in a daily matrix simultaneously (and then looping through the days (matrices)).
From what I have read, you can use a cellfun if your matrix is a cell array (mine is not).
Then I was also looking at the bsxfun option, but it seems like the functions are just standard functions..like mean, max etc.
So now, I'm looking at using arrayfun in conjunction with a function I create from my algorithm to calculate degree days.
I have been trying to write a test function but Matlab keeps throwing up the same error:
I type:
function output=degreedays(x)
and Matlab throws back:
Error: Function definitions are not permitted in this context.
Can someone tell me what I'm doing wrong? Why is it not accepting the declaration of the function name?
MATLAB does not allow you to define named functions like this at the command line. You need to place your function definition in a file. MATLAB then can call that function by the name of the file - so in your case, put your function definition in a file called degreedays.m.
See the doc for more: https://uk.mathworks.com/help/matlab/matlab_prog/create-functions-in-files.html .

CRC16 hash function that calculates hash value from two inputs

I want to write a CRC16 hash function that takes two inputs and generate a hash value from them. The current implementations calculate take only one input.
current:
crc16(input_value)
required:
crc16(input_value1, input_value2)
One solution can be calculating the hash value for each input separately and then xor them. However, I don't know what would be the effect on randomness. Is XORing enough, or I should take another approach?
A more complete description of a CRC function would be:
new_crc_value = crc16(previous_crc_value, input_value)
Then to process two input values, you simply process them in sequence:
new_crc_value = crc16(crc16(previous_crc_value, input_value1), input_value2)

Multiple output of bootstrap in MATLAB

I have a function M file defined as follows:
function [v,m ] = myfun(y)
m=mean(y);
v=var(y);
end
For a given vector which consists of integers from 1 to 100 for simplicity, I want to do bootstrap for 10 times and obtain both mean and variance for each bootstrapped sample. The following wouldn't work:
y=[1:100]';
[m,v]=bootstrp(10,#(x) myfun(x),y);
Could any one help me out of here? Thanks in advance!
Why don't you think it works? This does exactly what you're specifying. However, I would do away with specifying a separate function and putting the mean and standard deviation directly in the anonymous function itself. Specifically:
stats = bootstrp(10, #(x) [mean(x) var(x)], y);
In this case, you will get a 10 x 2 matrix. The first column will give you the mean of each boostrapped sample while the next column will give you the variance of each bootstrapped sample. Specifically, the first row gives you the mean (first column) and variance (second column) of the first sample. The second row gives you the mean and variance of the second sample, and so on. Each column of your output in stats will give you whatever measure you are calculating in the corresponding position in the output vector of your function.
Check the documentation for bootstrp here: http://www.mathworks.com/help/stats/bootstrp.html
To answer your question as to why you're getting the too many outputs error is because you need to output only one variable, but you are outputting two. As such, group your variables into a single vector like so:
function [out] = myfun(y)
m=mean(y);
v=var(y);
out = [m,v];
end
If you now run your bootstrp code with this function, it should now work.

matlab zplane function: handles of vectors

I'm interested in understanding the variety of zeroes that a given function produces with the ultimate goal of identifying the what frequencies are passed in high/low pass filters. My idea is that finding the lowest value zero of a filter will identify the passband for a LPF specifically. I'm attempting to use the [hz,hp,ht] = zplane(z,p) function to do so.
The description for that function reads "returns vectors of handles to the zero lines, hz". Could someone help me with what a vector of a handle is and what I do with one to be able to find the various zeros?
For example, a simple 5-point running average filter:
runavh = (1/5) * ones(1,5);
using zplane(runavh) gives an acceptable pole/zero plot, but running the [hz,hp,ht] = zplane(z,p) function results in hz=175.1075. I don't know what this number represents and how to use it.
Many thanks.
Using the get command, you can find out things about the data.
For example, type G=get(hz) to get a list of properties of the zero lines. Then the XData is given by G.XData, i.e. X=G.XData.
Alternatively, you can only pull out the data you want
X=get(hz,'XData')
Hope that helps.

For iterator (loop)

I am trying to simulate throw of the ball under angles using simulink. I'm able to simulate it for one angle but I would like to simulate it using loop. This is what I want to do in simulink using FOR :
for i=-5:10:85
Here is picture of my simulink:
If I understand your question correctly, you essentially want to rerun your simulation multiple times for different values of the constant Degrees. Instead of using a For Iterator, you may be able to achieve effectively the same result by using vector operations. That is to say, change the value of the constant Degrees from being a scalar value to instead being a vector (in this particular case just set its value to be [5:10:85]). The outputs of your Simulink model (ie the x and y results) should now be vectors corresponding to the various Degree values.
Put all the blocks into the for-iterator subsystem. The For Iterator block will output the current iteration, you can use that index (which starts at 0/1) to cycle the angle from -5 to 85 (try to hook the For Iterator block up to a Gain and Sum block). At each iteration, all the blocks in the for-iterator subsystem will run, and the output of the For Iterator block will increment by one.
The previous solution to make the angles a vector will also work.
Using MATLAB's for reference page, I'd rewrite your line as:
for i=5:10:85
...
end