Random vector between 0 and 1 - octave

To generate a vector 1-20 I use :
a = [1:20]'
How to generate a vector of size 20 where each element is between 0-1 ?
Closest I've got is a = [rand():rand()]'
But how to generate a range ?

You can simply do:
v = rand(20, 1)
You can then sort the vector:
s = sort(v)

Related

How to plot a 2d Function in MATLAB

I am trying to plot a simple equation in MATLAB.
The equation is
z = x^2 - y^2, for -3 <= x <= 3, -3 <= y <= 3.
The current code that I have is
x = -3:3;
y = -3:3;
z = (x.^2) - (y.^2);
plot(z)
The result is
Please help me in this case because I am not sure if the code and graph is correct. Thank you very much.
This is not a piecewise function. A Piecewise Function is a function defined by multiple sub-functions, where each sub-function applies to a different interval in the domain. There is only one function here that takes two arrays of the same length. The calculations yield a vector of zeros, due to the input arrays. If you change either one of the vectors, that is "x" or "y", you will see a nonzero plot. Your code works as expected.
There is a lot going wrong here: Let's start at the beginning:
x = -3:3;
y = -3:3;
If we evaluate these they both will return an vector of integers:
x =
-3 -2 -1 0 1 2 3
This means that the grid on which the function is evaluated is going to be very coarse. To alleviate this you can define a step size, e.g. x = 3:0.1:3 or use linspace, in which case you set the number of samples, so e.g. x = linspace(-3, 3, 500). Now consider the next line:
z = (x.^2) - (y.^2);
If we evaluate this we get
z =
0 0 0 0 0 0 0
and you plot this vector with the 2d-plotting function
plot(z)
which perfectly explains why you get a straight line. This is because the automatic broadcasting of the arithmetic operators like minuse (-) just subtracts values entry-wise. You however want to evaluate z for each possible pair of values of x and y. To do this and to get a nice plot later you should use meshgrid, and use a plotting function like mesh to plot it. So I'd recommend using
[X,Y] = meshgrid(x,y);
to create the grid and then evaluate the function on the grid as follows
Z = X.^2 - Y.^2;
and finally plot your function with
mesh(X,Y,Z);

Input vector to a symbolic system of functions

Suppose that I have written a function containing 3 separable functions(a system of equations).
I want to calculate the value of this function for 3 different values of my variables but I do not want to use "subs" function. What I want to do is enter a vector containing the desired values of my variables and calculate the main function which is a vector. How could I do that?. Notice that I do not want to call the function by each variable. Here is my code:
syms x y z
f1 = symfun(x.^2+3.*x.*y,[x,y,z]);
f2 = symfun(z.^3+y-x.^3-12,[x,y,z]);
f3 = symfun(2*z+x.*y+z.*x+1,[x,y,z]);
f = [f1;f2;f3];
What I mean is to calculate the f function by for example: f([12 4 6]) not byf(12 4 5)
Not the most elegant, but the best I could think of at the moment is to put it in a function wrapper. This might be one way to pass inputs as an array. This will bridge the gap between indexing the array input to be passed into the symfcn (symbolic functions).
f([12 4 6])
function [Results] = f(Inputs)
syms x y z
f1(x,y,z) = x.^2+3.*x.*y;
f2(x,y,z) = z.^3+y-x.^3-12;
f3(x,y,z) = 2*z+x.*y+z.*x+1;
Sym_Functions = [f1;f2;f3];
Results = Sym_Functions(Inputs(1),Inputs(2),Inputs(3));
end

Convert from one range to another

I have two sets of ranges that I need to be translated from one to the other.
The first range is -100 ↔ 100 with 0 being default.
The second range is 0.0 ↔ 10.0 with 1 being default.
I am working in AS3 with the first range and the second range is a python class and I need these numbers to line up.
I am adjusting brightness of a video in realtime with a slider. The video filter accepts values between -100 ↔ 100. I need to then take that value and pass it to a python script but it only accepts values from 0.0 ↔ 10.0
I tried this function I found on the net, but it doesn't translate the values correctly in this particular case.
private function convertRange(originalStart:Number,originalEnd:Number,newStart:Number,newEnd:Number,value:Number):Number
{
var originalRange:Number = originalEnd - originalStart;
var newRange:Number = newEnd - newStart;
var ratio:Number = newRange / originalRange;
var newValue:Number = value * ratio;
var finalValue:Number = newValue + newStart;
return finalValue;
}
Is this even possible? Hopefully my question is clear, please let me know if it needs clarification.
This is the python class I am referring to: https://github.com/dubhater/vapoursynth-adjust It uses the second range whereas AS3 uses the first range.
Why not trying something like this :
function from_AS_to_PY(as_value:Number): Number // as_value : -100 ----- 0 ----- 100
{
var py_value:Number = (as_value / 100);
py_value = (py_value <= 0 ? py_value : py_value * 9) + 1;
return py_value;
}
function from_PY_to_AS(py_value:Number): Number // py_value : 0 - 1 --------- 10
{
var as_value:Number = (py_value <= 1 ? py_value - 1 : ((py_value - 1) / 9)) * 100;
return as_value;
}
trace(from_AS_to_PY(-100)); // gives : 0
trace(from_AS_to_PY(-99)); // gives : 0.01
trace(from_AS_to_PY(-1)); // gives : 0.99
trace(from_AS_to_PY(0)); // gives : 1
trace(from_AS_to_PY(1)); // gives : 1.09
trace(from_AS_to_PY(99)); // gives : 9.91
trace(from_AS_to_PY(100)); // gives : 10
//---------------------------------------------------
trace(from_PY_to_AS(0)); // gives : -100
trace(from_PY_to_AS(0.01)); // gives : -99
trace(from_PY_to_AS(0.99)); // gives : -1
trace(from_PY_to_AS(1)); // gives : 0
trace(from_PY_to_AS(1.09)); // gives : 1
trace(from_PY_to_AS(9.91)); // gives : 99
trace(from_PY_to_AS(10)); // gives : 100
Hope that can help.
There is a fundamental difficulty with the problem. You are trying to fit three point of one range onto another range. If you were just interested in matching the two end points that would be easy you can use a linear interpolation y = 10 * (x+100) /200 or simply y = (x+100)/20 or equivalently y=x/20+5. The problem is it this does not match the default value and x=0 -> 5.
This might be the solution you want. However if it is important that he default values match you need to use a non-linear solution. There are many possible solutions. You can use a piecewise-linear solution like akmozo solution, which needs an if statement. if x<0 then y = x/100+1 else y = 1 + 9 x /100. The problem with this one is that you do not get a smooth response. Consider adjusting the slider from min to max, you see a very slow increase in brightness to start with and then it starts to increase much faster once you pass zero.
The big difference between the first half of the range and the second half suggests an exponential type solution. y = A exp(b x). Taking y = exp(x * ln(10)/100) matches the center point and the top end, the bottom end is just a little bit high at 0.1, rather than zero. This might be fine, if not you could find an exponential solution y = A exp(b x)-c which matches all three points.
Another possibility is using a power. An equation like y = A pow(x,n). A bit of calculation shows y=10 pow(x/200+0.5),3.321928095) matches all three points. The constant 3.321928095 = ln(0.1)/ln(0.5).
In the diagram the black curve is a simple linear solution. The red curve is the piecewise linear one, the green is the exponential one then the blue is the power.

julia JSON.parse losing type information

I just started to learn julia but I have this issue:
I am trying to use julia's JSON.parse to parse a matrix (a list of coordinates) but it is losing the type information.
coords = JSON.parse("[[1.0,-2.0],[3.0,4.0],[5.0,-1.2]]")
it is returning the Any type instead of the Float type:
3-element Array{Any,1}:
{1.0,-2.0}
{3.0,4.0}
{5.0,-1.2}
How do i get (or convert this) to an Array of floats?
Edit.
Here is the larger problem:
taxi_df = readtable("./test.csv")
coords = [JSON.parse(x) for x in taxi_df[:POLYLINE]]
times = [float(length(x)*15) for x in coords]
df_submission = DataFrame()
df_submission[:TRIP_ID] = taxi_df[:TRIP_ID]
mean_time = mean(times)
df_submission[:TRAVEL_TIME] = [max(x, mean_time) for x in times]
writetable("submission.csv", df_submission)
I think its doing that in the first place because that data is a list-of-lists in JSON, so it can't convert to a matrix.
You can do
float(hcat(coords...))
if those are columns, or
float(hcat(coords...))'
if they are rows. If efficiency is critical for this code, can also just preallocate the output matrix and use a for loop, e.g.
A = zeros(3,2)
for i in 1:3, j in 1:2
#inbounds A[i,j] = coords[i][j]
end

PDF Functiontype 2 exponential interpolation equation

I'm modifying an application that does PDF manipulation. The function written to interpret functiontype 2s seems to produce a negative value far too frequently. The equation found in the PDF spec is:
yj = C0j + x^N × (C1j − C0j), for 0 ≤ j < n.
Now, I'm getting negative values when I process a function with C0=1 and C1=0. I'm wondering if this is because I'm setting my own x value. What is the x value supposed to be?
The function is computed like this:
1. Input value is cut to function domain:
input = input > domainMax ? domainMax : (input < domainMin ? domainMin : input)
2. Compute inputN = input^N
3. Compute each component of the output value (1 component in your situation) using the formula:
output[j] = C0[j] + inputN * (C1[j] − C0[j]), for 0 ≤ j < n.
4. Cut each output component to function range:
output = output > rangeMax ? rangeMax : (output < rangeMin ? rangeMin : output)
Your function is a linear function (indeed can be used to define a gradient) and returns 0 when input is 1 and 1 when input is 0. Input values greater than 1 are invalid and they are cut to 1 before using them in calculation because the domain is [0, 1].
The application that parses spot color percentages might have to divide that percentage by 100 before feeding it to the function.