how to plot with vectical lines with gnuplot? - visualization

I study for sometime but didn't get the answer yet.
The data is like:
#X0, Y0_0, Y0_1
1 1 2
3 2 4
7 1 3
....
I need to draw vectical line from (X0, Y0_0) to (X0, Y0_1). gnuplot has financebar and candlesticks but they are too much.
I just want a single vectical line for each record.
Appreciate for any help.

If I understand correctly what you are after, the following script should do the job:
set offsets 1, 1, 1, 1
set key off
plot "-" u 1:2:(0):($3) w vectors nohead
1 1 2
3 2 4
7 1 3
e
What it does:
It sets the offset, so that you can see the left and right vector, so that it is not hidden by the axis.
Remove the label, since it has no use in this example
Plots vectors with no head (a single vertical line). The "parameters" are as stated here the x y dx dy. The (0) stands for dx=0 and the brackets are important. Otherwise the column 0 would be used which in gnuplot is the index of the data (line number).
Below the plot you will get with the script above:

Related

breeze Copy lower triangle not working?

Using breeze 0.13, Scala 2.12.3
I would like to create a symmetric matrix by copying the lower triangle to the upper one. Following the instructions in Linear Algebra Cheat Sheet. May be I use the function incorrectly but it seems like the lowerTriangular function is not working correctly. The copy just
val myMtx = breeze.linalg.DenseMatrix((1,-8,-9) , (2,1,-7), (3,5,1))
//myMtx: breeze.linalg.DenseMatrix[Int] =
// 1 -8 -9
// 2 1 -7
// 3 5 1
Copy lower triangle (seems not working)
breeze.linalg.lowerTriangular(myMtx)
// 1 0 0
// 2 1 0
// 3 5 1
Copy upper triangle (not working either)
breeze.linalg.upperTriangular(myMtx)
// 1 -8 -9
// 0 1 -7
// 0 0 1
In this context, "copy" means returns the matrix in a newly allocated memory. This is contrast with a "view" that indexes into the same backing memory.
With that in mind, both lowerTriangular and upperTriangular seem to be working properly in that it returns a new matrix that has the same elements copied from the original matrix.
The task is now to create a new symmetric matrix that is the copy of the lower triangular. One possible way is to compute the element-wise sum of the lower and lower diagonal transpose, and then subtract the extra copy of the diagonal that was computed. The inner diag() returns a view, as described above, as a vector and that vector is then used to create a diagonal matrix with the second diag().
# val sym = lowerTriangular(myMtx) + lowerTriangular(myMtx).t - diag(diag(myMtx))
sym: DenseMatrix[Int] =
1 2 3
2 1 5
3 5 1

Remove duplicates appearing next to each other, but keep it if it appears again later

I have a vector that could look like this:
v = [1 1 2 2 2 3 3 3 3 2 2 1 1 1];
that is, the number of equal elements can vary, but they always increase and decrease stepwise by 1.
What I want is an easy way to be left with a new vector looking like this:
v2 = [ 1 2 3 2 1];
holding all the different elements (in the right order as they appear in v), but only one of each. Preferably without looping, since generally my vectors are about 10 000 elements long, and already inside a loop that's taking for ever to run.
Thank you so much for any answers!
You can use diff for this. All you're really asking for is: Delete any element that's equal to the one in front of it.
diff return the difference between all adjacent elements in a vector. If there is no difference, it will return 0. v(ind~=0) will give you all elements that have a value different than zero. The 1 in the beginning is to make sure the first element is counted. As diff returns the difference between elements, numel(diff(v)) = numel(v)-1.
v = [1 1 2 2 2 3 3 3 3 2 2 1 1 1];
ind = [1 diff(v)];
v(ind~=0)
ans =
1 2 3 2 1
This can of course be done in a single line if you want:
v([1, diff(v)]~=0)
You could try using diff which, for a vector X, returns [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)] (type help diff for details on this function). Since the elements in your vector always increase or decrease by 1, then
diff(v)
will be a vector (of size one less than v) with zeros and ones where a one indicates a step up or down. We can ignore all the zeros as they imply repeated numbers. We can convert this to a logical array as
logical(diff(v))
so that we can index into v and access its elements as
v(logical(diff(v)))
which returns
1 2 3 2
This is almost what you want, just without the final number which can be added as
[v(logical(diff(v))) v(end)]
Try the above and see what happens!

MATLAB Combine matrices of different dimensions, filling values of corresponding indices

I have two matrices, 22007x3 and 352x2. The first column in each is an index, most (but not all) of which are shared (i.e. x1 contains indices that aren't in x2).
I would like to combine the two matrices into a 22007x4 matrix, such that column 4 is filled in with the values that correspond to particular indices in both original matrices.
For example:
x1 =
1 1 5
1 2 4
1 3 5
2 1 1
2 2 1
2 3 2
x2 =
1 15.5
2 -5.6
becomes
x3 =
1 1 5 15.5
1 2 4 15.5
1 3 5 15.5
2 1 1 -5.6
2 2 1 -5.6
2 3 2 -5.6
I've tried something along the lines of
x3(1:numel(x1),1:3)=x1;
x3(1:numel(x2(:,2)),4)=x2(:,2);
but firstly I get the error
??? Subscripted assignment dimension mismatch.
and then I can't figure out I would fill the rest of it.
An important point is that there are not necessarily an equal number of rows per index in my data.
How might I make this work?
Taking Amro's answer from here
[~, loc] = ismember(x1(:,1), x2(:,1));
ismember's second argument returns the location in x2 where each element of x1 can be found (or 0 if it can't)
a = x2(loc(loc > 0), 2);
get the relevant values using these row indices but excluding the zeros, hence the loc > 0 mask. You have to exclude these as 1, they are not in x2 and 2 you can't index with 0.
Make a new column of default values to stick on the end of x1. I think NaN() is probably better but zeros() is also fine maybe
newCol = NaN(size(x1,1),1)
Now use logical indexing to get the locations of the non zero elements and put a in those locations
newCol(loc > 0) = a
Finnaly stick it on the end
x3 = [x1, newCol]

Plotting a normpdf in 3D over scatter plot/linear regression

I have a set of data x(1000,2),
2 18.1199158925616
3 9.56472607516328
7 34.3629828698699
4 30.9865760010538
7 54.4869549747429
8 54.7696742935546
2 1.45832407805944
3 15.4231700658276
8 57.0479651194063
1 5.34909555648445
2 11.5593766410445
2 17.4392978010484
6 25.4072607558367
4 15.1248062077068
2 13.2035176450795
7 33.2507069131440
5 21.6859046133920
3 18.4458236149053
1 0.824962982951128
8 46.6322862041701
8 43.6889420657037
2 17.0930689315346
3 13.9754117410459
2 1.99260423380962
11 86.3172411757665
8 50.6244869641866
1 1.16602336459361
2 12.6604166373800
2 17.4519078987572
5 27.4799515521804
which I have plotted plot(x(:,1),x(:,2),'*r');in scatter form:
but would like to have something like below using normpdf for selected values of x(:,1):
To find values in x(:,2) column corresponding to x(:,1)==3:
[x3]=x(find(x(:,1)==3),2);
mu=mean(x3);
sig=std(x3);
Y=normpdf(x3,mu,sig);
figure('color',[1,1,1]);
hhh=plot3(3*ones(length(xi)),x(find(x(:,1)==3),2),Y,'-r')
p = polyfit(x(:,1),x(:,2),1);
plin=plot(x(:,1),polyval(p,x(:,1)),'-b');
but how do I get the red dotted pdf to be a smooth curve as '-r' gives me zig-zag lines. Do I have to order the data in some way?
Also how do I get the red dots in the center of the normal curve down to the blue line?
And perhaps most importantly, as a well informed reader would you like to see this type of plot or perhaps something else less fancy?
Yes, a sorting of the x-values should get you the red curve. Also you do not need find() here. I have not tested the code, but these changes should work:
[x3]=sort(x(x(:,1)==3,2));
mu=mean(x3);
sig=std(x3);
Y=normpdf(x3,mu,sig);
figure('color',[1,1,1]);
hhh=plot3(3*ones(length(xi)),x3,Y,'-r')
p = polyfit(x(:,1),x(:,2),1);
plin=plot(x(:,1),polyval(p,x(:,1)),'-b');
As for the value of such a plot, I would guess that it will be quite cluttered. But I guess it could be used to give a general view of the distribution of the data.

Confuse about Hist default function in matlab

For example i have an image matrix that looks like this one:
1 2 3
1 5 6
1 5 3
Assume that my intensity goes from 1 to 6, so I need to produce something like an array:
1 : 3
2 : 1
3 : 2
4 : 0
5 : 2
6 : 1
I tried using hist function of matlab but it produced something looks really weried to me.
Can anybosy help me on that? Thank you very much
The key here is to linearize the image matrix using the colon operator, like in hist(a(:)). Calling hist(a) when a is a matrix will compute the histogram of each column.
I suspect that you probably haven't specified the correct histogram bins for hist (by default it creates 10 equally spaced bins). If you're interested in a simple histogram count, consider using histc:
vals = 1:max(A(:));
count = histc(A(:), vals);
where A is your image matrix. vals is the first column in your desired output array, and count is the the second.