How to set data in chart with interval? - charts

I use Google Chart API.
My array contents some parameters for displaying:
var rawData = [
[{v: [8, 0, 0], f: '03/02/13'}, 1],
[{v: [9, 0, 0], f: '04/02/13'}, 2],
[{v: [10, 0, 0], f:'05/02/13'}, 3]
];
How to display only one title for 3 element no all from array?

It's hard to tell from the wording of the question, but I think you're looking for something like a Data View? They allow you to filter a data table. Select only certain rows or columns, etc. If you only ever have the 3 rows you might also consider just having an array of 3 DataTables and cycling through them, redrawing the chart for each.

Related

Seeking vectorized solution to sum up elements using accumarray in Matlab/Numpy

(To anyone who reads this, just to not waste your time, I wrote up this question and then came up with a solution to it right after I wrote it. I am posting this here just to help out anyone who happened to also be thinking about something like this.)
I have a vector with elements that I would like to sum up. The elements that I would like to add up are elements that share the same "triggerNumber". For example:
vector = [0, 1, 1, 1, 1]
triggerNumber = [1, 1, 1, 2, 2]
I will sum up the numbers that share a triggerNumber of 1 (so 0+1+1 =2) and share a triggerNumber of 2 (so 1+1+1 = 3). Therefore my desiredOutput is the array [2, 2].
accumarray accomplishes this task, and if I give it those two inputs:
output = accumarray(triggerNumber.',vector.').'
which returns [2, 2]. But, while my "triggerNumbers" are always increasing, they are not necessarily always increasing by one. So for example I might have the following situation:
vector = [0, 1, 1, 1, 1]
triggerNumber = [4, 4, 4, 6, 6]
output = accumarray(triggerNumber.',vector.').'
But now this returns the output:
output = [0, 0, 0, 2, 0, 2]
Which is not what I want. I want to just sum up elements with the same trigger number (in order), so the desired output is still [2, 2]. Naively I thought that just deleting the zeros would be sufficient, but then that messes up the situation with the inputs:
vector = [0, 0, 0, 1, 1]
triggerNumber = [4, 4, 4, 6, 6]
which if I deleted the zeroes would return just [2] instead of the desired [0, 2].
Any ideas for how I can accomplish this task (in a vectorized way of course)?
I just needed to turn things like [4, 4, 4, 6, 6] into [1, 1, 1, 2, 2], which can be done with a combination of cumsum and diff.
vector = [0, 0, 0, 1, 1];
triggerNumber = [4, 4, 4, 6, 6];
vec1 = cumsum(diff(triggerNumber)>0);
append1 = [0, vec1];
magic = append1+1;
output = accumarray(magic.',vector.').'
which returns [2, 2]....and hopefully my method works for all cases.

MATLAB: Subsetting one array by values of a second

I basically have two columns (arrays): column A represents a continuous stream of data across points in time (e.g. blood pressure rising and falling), while column B represents onset of an event (e.g. a shock or a deep breath). Column A has values for every cell, while column B only has values at a time point where an event occurred, which represent codes for the onset of different kinds of events (e.g. 1, 2, 3, 4, 5 for 5 kinds).
What code can use the values in column B to subset data in column A (say collect all data from any time points between an event 1 and 2, and event 1 and 3, or event 1 and 4)? Basically, I'm trying to pull out the values for only certain time period segments, and store them in a cell array.
Example:
What I have:
Array A: 10, 12, 13, 20, 15, 16, 14, 9, 8, 11, 12, 15, 14
Array B: 1, 0, 2, 0, 0, 0, 1, 0, 2, 0, 1, 0, 2
*(where in Array B, 1 and 2 are events--say a showing a cue and a subject
responding to that cue--and I want the data between a 1 and a 2)*
What I want:
(New) Cell Array C: [12, 13] , [9, 8] , [15,14]
*That is, it's grabbing the data from Array A, based on what falls between
1s and 2s in Array B, and storing them into cells of Array C*
Many thanks!
Here's a way:
Find the indices of starts and ends. This can be done using strfind, which also works with numeric arrays.
Use those indices to build the result with a loop.
ind_start = strfind(B, [1 0]); % Or: ind_start = strfind(char(B+48), '10');
ind_end = strfind(B, 2); % Or: ind_end = strfind(char(B+48), '2')
result = cell(size(ind_start));
for k = 1:numel(ind_start)
result{k} = A(ind_start(k)+1:ind_end(k));
end

How to see the contents of each partition in an RDD in pyspark?

I want to learn a little more about how pyspark partitions data. I need a function such that:
a = sc.parallelize(range(10), 5)
show_partitions(a)
#output:[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]] (or however it partitions)
The glom function is what you are looking for:
glom(self): Return an RDD created by coalescing all elements within each partition into a list.
a = sc.parallelize(range(10), 5)
a.glom().collect()
#output:[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
val data = List((1,3),(1,2),(1,4),(2,3),(3,6),(3,8))
val rdd = sc.parallelize(data)
rdd.glom().collect()
.foreach(a => {
a.foreach(println);
println("=====")})
in this way , you can check how the data is partitioned

Expand an array by filling in with current values in MATLAB

I have a fairly simple issue and I just want to know if there's an easy way to do it in MATLAB (i.e. a function to do this rather than writing out loops or something myself).
Let's say I have a timeseries where Time is 1:1:1000 and Data is 2 * (1:1:1000) and I want to expand the array by making the Time and Data vector finer. Let's say that I want Time to be 1:0.1:1000 and Data to be 2 * (1:0.1:1000). Is there an easy way to tell MATLAB that to repeat the values of each vector 10 times (from 1 / 0.1 = 10) so that I can have something like this?:
Time: [1, 2, 3, 4, ...]
Data: [2, 4, 6, 8, ...]
to:
Time: [1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, ...]
Data: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, ...]
You can use combination of reshape() and repmat() as follow:
Data = [2, 4, 6, 8, ...] % As stated in the question.
Data = reshape(repmat(Data, 10, 1), 1, []);
This is more time-efficient than the others like kron() or combination of sort() and repmat().
Two simulations were done and the results are shown in the following figures.
First: Simulation time vs. length of Data. Here I used N=100 instead of 10.
Second: Simulation time vs. repetition factor. Length of Data is 10000.
So you can select the best one according to the simulation results.
As seb proposed, you can use the function repmat. Here what I would do:
Data = [2, 4, 6, 8, ...];
Data = sort(repmat(Data,1,10));
You can use repmat
interval_size = 10;
Data = 2*(1:1:1000);
out_data = repmat(Data,interval_size,1);
out_data = out_data(:)';
Example Data:
time=1:50
data=2:2:100
t2=1:.1:50.9
For time=1:n this is very simple:
data(:,floor(t2))
If your original data has another time scale, use this:
[a,b]=ismember(floor(t2),time)
data(:,b)

Create array of points from single dimensional array of points

Waht i need to do is take a single dimensional array, ie:
[1, 1, 2, 2, 3, 3]
and turn it into an array of points:
[[1, 1], [2, 2], [3, 3]]
I am hoping for a simple native matlab way of doing it rather then a function. This will be going into sets of points ie:
[ [[1, 1], [2, 2], [3, 3]],
[[4, 4], [5, 5], [6, 6]],
[[7, 7], [7, 7], [8, 8]] ]
The reason this is going to happen is the points will be stored in a text file as a single stream and i need to turn them into something meaningful.
First note that a horizontal concatenation of row vectors will result in one larger row vector rather than in a row of pairs, that is [[1, 1], [2, 2], [3, 3]] is the same as [1 1 2 2 3 3]. Hence, you need to concatenate them vertically.
You can try
a = [1, 1, 2, 2, 3, 3];
b = reshape(a, 2, floor(length(a)/2))';
This will result in a matrix where each row represents the coordinates of one point.
b =
1 1
2 2
3 3
I'm just adding this answer for the sake of diversity:
Just as H.Muster said, concatenation of vectors will result in a larger vector or a matrix (depending on your operation). You can go with that.
But you can also use a cell array, which is a set of data containers called "cells". A cell can contain any type of data, regradless of what other cells contain in the same cell array.
In your case, creating a cell array can be done using a slightly different syntax (than H.Muster's answer):
a = [1, 1, 2, 2, 3, 3];
p = mat2cell(a, 1, 2 * ones(1, numel(a) / 2))
p is a cell array, each cell containing a 1-by-2 point vector. To access an element in a cell array, you'll have to use curly braces. For instance, the second point would be p{2} = [2, 2].