Converting a single col into mutiple rows (GNUPLOT file) - bash

I'm trying to make a gnuplot picture from a file, have a problem with the distribution of 'data.txt' file. The actual distribution is :
1 4 x1
1 16 x2
4 4 x3
4 16 x4
8 4 x5
8 16 x6
The first line makes reference to the number of lines that i want, and the other colums make reference to the x and y axis.
I'm trying two approximations to make the picture without success:
Use some gnuplot function to sketch the picture using the actual distribution that my file have. I have not found this command.
Make a bash script to convert the actual file into another with the correct distribution
4 x1 x3 x5
16 x2 x4 x6

Addressing #2
awk '{a[$2] = a[$2] $3 " "} END {for (i in a) print i, a[i]}' file
4 x1 x3 x5
16 x2 x4 x6

Related

Truth table of f(x1,x2,x3,x4) function from given two (4-1) multiplexers

Given two (4-1) multiplexers
How can I get the truth table of f(x1,x2,x3,x4) function??
A 4-1 multiplexer has the following general truth-table:
A1 A0 Y
0 0 I0
0 1 I1
1 0 I2
1 1 I3
The two control inputs A0 and A1 select which of the four inputs is switched through to the output.
To get your question solved, start with the left-hand multiplexer and write a truth-table for it.
In a second step write the overall truth-table by plugging in the intermediate signal values in the general table shown above.
The resulting truth-table has four input columns X1, X2, X3, X4.
There is one output column Y. Rather than using intermediate truth-tables you could figure out the output value for each of the 16 input combinations.

Algorithm to assign one activity to each one individual in order to maximize the sum of the affinity scores

Consider N individuals (x1, x2 ... xn) and M activities (y1, y2 ... ym).
For each pair (individual, activity) there is a score of affinity, a that relates them.
The objective is to assign an activity to each individual to maximize the sum of the affinity scores of the chosen pairs. An activity can be assigned to only one individual and an individual can be associated with only one activity.
Below is an example of a matrix that reports the affinity of each pair.
| x1 x2 x3
----+------------
y1 | 2 7 0
y2 | 7 7 9
y3 | 3 1 4
y4 | 5 4 4
I would like to know efficient algorithms that allow finding these pairs (individual, activity) that maximize the sum of affinities.
PS: I'm not an expert, so forgive me if that can have an obvious answer.

system of linear equation with additional constraint of minimal variables sum

I have a system of linear equations that I already reduced to a row echelon matrix using Gauss-Jordan elimination. My system with n variables Xn (where Xn is in N0 (=positive integers)) has multiple solutions and I want to find the solution for witch the Sum of all Xn is minimal.
How could I do that programmatically?
For example consider this system of linear equations:
x1 + + x5 + x6 = 2
x2 + x5 = 1
x3 + x6 = 1
x4 + x5 + x6 = 1
one of the minimal solution I want to obtain is:
x3 = x4 = x5 = 0
x1 = x2 = x6 = 1
another one would be
x2 = x4 = x6 = 0
x1 = x3 = x5 = 1
But I don't want
x1 = 2
x2 = x3 = x4 = 1
x5 = x6 = 0
which is also a solution of this system, but not a minimal one according to my criteria as x1 + x2 + x3 + x4 + x5 + x6 = 5 (whereas it is only 3 for the 2 first solutions)
In case of multiple minimal solutions (like here, where solutions 1 and 2 are both minimal), I don't care about the minimal solution that is returned as long as it is one of the minimal ones
Since the variables are all nonnegative, this problem is essentially equivalent to integer programming. Use an off-the-shelf integer program solver and formulate like
minimize x1 + x2 + x3 + x4 + x5 + x6
subject to
x1 + x5 + x6 = 2
x2 + x5 = 1
x3 + x6 = 1
x4 + x5 + x6 = 1
integers
x1, x2, x3, x4, x5, x6 >= 0
(exact syntax depends on the tool).

Bilinear image interpolation / scaling - A calculation example

I would like to ask you about some bilinear interpolation / scaling details. Let's assume that we have this matrix:
|100 | 50 |
|70 | 20 |
This is a 2 x 2 grayscale image. Now, I would like scale it by factor of two and my matrix looks like this:
| 100 | f1 | 50 | f2 |
| f3 | f4 | f5 | f6 |
| 70 | f7 | 20 | f8 |
so if we would like to calculate f4, the calculation is defined as
f1 = 100 + 0.5(50 - 100) = 75
f7 = 70 + 0.5(20 - 70) = 45
and now finally:
f4 = 75 + 0.5(45 - 75) = 60
However, I can't really understand what calculations are proper for f3 or f1
Do we do the bilinear scaling in each direction separately? Therefore, this would mean that:
f3 = 100 + 0.5(70 - 100) = 85
f1 = 100 + 0.5(50 - 100) = 75
Also, how should I treat f2, f6, f8. Are those points simply being copied like in the nearest neighbor algorithm?
I would like to point you to this very insightful graphic from Wikipedia that illustrates how to do bilinear interpolation for one point:
Source: Wikipedia
As you can see, the four red points are what is known. These points you know before hand and P is the point we wish to interpolate. As such, we have to do two steps (as you have indicated in your post). To handle the x coordinate (horizontal), we must calculate what the interpolated value is row wise for the top row of red points and the bottom row of red points. This results in the two blue points R1 and R2. To handle the y coordinate (vertical), we use the two blue points and interpolate vertically to get the final P point.
When you resize an image, even though we don't visually see what I'm about to say, but imagine that this image is a 3D signal f. Each point in the matrix is in fact a 3D coordinate where the column location is the x value, the row location is the y value and the z value is the quantity / grayscale value of the matrix itself. Therefore, doing z = f(x,y) is the value of the matrix at location (x,y) in the matrix. In our case, because you're dealing with images, each value of (x,y) are integers that go from 1 up to as many rows/columns as we have depending on what dimension you're looking at.
Therefore, given the coordinate you want to interpolate at (x,y), and given the red coordinates in the image above, which we call them x1,y1,x2,y2 as per the diagram - specifically going with the convention of the diagram and referencing how images are accessed: x1 = 1, x2 = 2, y1 = 2, y2 = 1, the blue coordinates R1 and R2 are computed via 1D interpolation column wise using the same row both points coincide on:
R1 = f(x1,y1) + (x - x1)/(x2 - x1)*(f(x2,y1) - f(x1,y1))
R2 = f(x1,y2) + (x - x1)/(x2 - x1)*(f(x2,y2) - f(x1,y2))
It's important to note that (x - x1) / (x2 - x1) is a weight / proportion of how much of a mix the output consists of between the two values seen at f(x1,y1) and f(x2,y1) for R1 or f(x1,y2) and f(x2,y2) for R2. Specifically, x1 is the starting point and (x2 - x1) is the difference in x values. You can verify that substituting x1 as x gives us 0 while x2 as x gives us 1. This weight fluctuates between [0,1] which is required for the calculations to work.
It should be noted that the origin of the image is at the top-left corner, and so (1,1) is at the top-left corner. Once you find R1 and R2, we can find P by interpolating row wise:
P = R2 + (y - y2)/(y2 - y1)*(R1 - R2)
Again, (y - y2) / (y2 - y1) denote the proportion / mix of how much R1 and R2 contribute to the final output P. As such, you calculated f5 correctly because you used four known points: The top left is 100, top right is 50, bottom left is 70 and bottom right is 20. Specifically, if you want to compute f5, this means that (x,y) = (1.5,1.5) because we're halfway in between the 100 and 50 due to the fact that you're scaling the image by two. If you plug in these values into the above computation, you will get the value of 60 as you expected. The weights for both calculations will also result in 0.5, which is what you got in your calculations and that's what we expect.
If you compute f1, this corresponds to (x,y) = (1.5,1) and if you substitute this into the above equation, you will see that (y - y2)/(y2 - y1) gives you 0 or the weight is 0, and so what is computed is just R2, corresponding to the linear interpolation along the top row only. Similarly, if we computed f7, this means we want to interpolate at (x,y) = (1.5,2). In this case, you will see that (y - y2) / (y2 - y1) is 1 or the weight is 1 and so P = R2 + (R1 - R2), which simplifies to R1 and is the linear interpolation along the bottom row only.
Now there's the case of f3 and f5. Those both correspond to (x,y) = (1,1.5) and (x,y) = (2,1.5) respectively. Substituting these values in for R1 and R2 and P for both cases give:
f3
R1 = f(1,2) + (1 - 1)/(2 - 1)*(f(2,2) - f(1,2)) = f(1,2)
R2 = f(1,1) + (1 - 1)/(2 - 1)*(f(1,2) - f(1,1)) = f(1,1)
P = R1 + (1.5 - 1)*(R1 - R2) = f(1,2) + 0.5*(f(1,2) - f(1,1))
P = 70 + 0.5*(100 - 70) = 85
f5
R1 = f(1,2) + (2 - 1)/(2 - 1)*(f(2,2) - f(1,2)) = f(2,2)
R2 = f(1,1) + (2 - 1)/(2 - 1)*(f(1,2) - f(1,1)) = f(1,2)
P = R1 + (1.5 - 1)*(R1 - R2) = f(2,2) + 0.5*(f(2,2) - f(1,2))
P = 20 + 0.5*(50 - 20) = 35
So what does this tell us? This means that you are interpolating along the y-direction only. This is apparent when we take a look at P. Examining the calculations more thoroughly of P for each of f3 and f5, you see that we are considering values along the vertical direction only.
As such, if you want a definitive answer, f1 and f7 are found by interpolating along the x / column direction only along the same row. f3 and f5 are found by interpolating y / row direction along the same column. f4 uses a mixture of f1 and f7 to compute the final value as you have already seen.
To answer your final question, f2, f6 and f8 are filled in based on personal preference. These values are considered to be out of bounds, with the x and y values both being 2.5 and that's outside of our [1,2] grid for (x,y). In MATLAB, the default implementation of this is to fill any values outside of the defined boundaries to be not-a-number (NaN), but sometimes, people extrapolate using linear interpolation, copy the border values, or perform some elaborate padding like symmetric or circular padding. It depends on what situation you're in, but there is no correct and definitive answer on how to fill in f2, f6 and f8 - it all depends on your application and what makes the most sense to you.
As a bonus, we can verify that my calculations are correct in MATLAB. We first define a grid of (x,y) points in the [1,2] range, then resize the image so that it's twice as large where we specify a resolution of 0.5 per point rather than 1. I'm going to call your defined matrix A:
A = [100 50; 70 20]; %// Define original matrix
[X,Y] = meshgrid(1:2,1:2); %// Define original grid of points
[X2,Y2] = meshgrid(1:0.5:2.5,1:0.5:2.5) %// Define expanded grid of points
B = interp2(X,Y,A,X2,Y2,'linear'); %// Perform bilinear interpolation
The original (x,y) grid of points looks like:
>> X
X =
1 2
1 2
>> Y
Y =
1 1
2 2
The expanded grid to expand the size of the matrix by twice as much looks like:
>> X2
X2 =
1.0000 1.5000 2.0000 2.5000
1.0000 1.5000 2.0000 2.5000
1.0000 1.5000 2.0000 2.5000
1.0000 1.5000 2.0000 2.5000
>> Y2
Y2 =
1.0000 1.0000 1.0000 1.0000
1.5000 1.5000 1.5000 1.5000
2.0000 2.0000 2.0000 2.0000
2.5000 2.5000 2.5000 2.5000
B is the output using X and Y as the original grid of points and X2 and Y2 are the points we want to interpolate at.
We get:
>> B
B =
100 75 50 NaN
85 60 35 NaN
70 45 20 NaN
NaN NaN NaN NaN

SPSS Return Highest Variable Name

1) I need SPSS to return the variable name of the highest variable in a series of subscales. Basically I have ten subscales with mean scores ranging from 0 to 5, I don't want to know the highest score for each case, but rather which subscale is highest.
When I use this syntax, I just get the score, which doesn't tell me which category it belongs to.
COMPUTE Motivation_Highest2 = MAX(Stress_Mgmt, Revitalisation, Enjoyment, Challenge, SocialRecog, Affiliation, Competition, HealthPress, IllHealth, PosHealth,
WtMgmt, Appearance, StrengthEnd, Nimbleness, MotivationHighest).
VARIABLE LABELS Motivation_Highest2 'Motivation Intensity: Highest Score on any Motivation Subscale'.
EXECUTE.
Can I ask SPSS to return the variable name instead of the score?
2) There may be two scores that are both equally high. In this case, I would like SPSS to give me both variable names.
Thanks!
This is an ok job for a macro to do.
DEFINE !MaxVars (OutN = !TOKENS(1)
/OutV = !CHAREND("/")
/Var = !CMDEND)
NUMERIC !OutN.
!DO !I !IN (!Var)
COMPUTE !OutN = MAX(!OutN,!I).
!DOEND
STRING !OutV (!CONCAT("A",!LENGTH(!Var))).
!DO !I !IN (!Var)
IF !I = !OutN !OutV = LTRIM(CONCAT(RTRIM(!OutV)," ",!QUOTE(!I))).
!DOEND
!ENDDEFINE.
And here is an example of using it on a set of data.
DATA LIST FREE / X1 X2 X3.
BEGIN DATA
1 2 3
3 2 1
4 4 0
0 4 4
1 1 1
END DATA.
!MaxVars OutN = Max OutV = Vars /Var = X1 X2 X3.
If you then run LIST Max Vars. it will return in the output:
Max Vars
3 X3
3 X1
4 X1 X2
4 X2 X3
1 X1 X2 X3

Resources