Core-Plot how to pack data - iphone

I was wondering how I need to prepare the data so it is ready for core-plot.
one line per year
dayOfYear in the x-axis
value on the y-axis
My x-axis has 366 points (Each day of the year). At the moment I have a dictionary that looks like this
2009 (year) = {
151 (dayofyear) = 5 (value);
192 = 25;
206 = 5;
234 = 20;
235 = 20;
255 = 20;
262 = 10;
276 = 10;
290 = 10;
298 = 7;
310 = 1;
338 = 3;
354 = 5;
362 = 5;
};
2010 = {
114 = 7;
119 = 3;
144 = 7;
17 = 5;
187 = 10;
198 = 7;
205 = 10;
212 = 10;
213 = 20;
215 = 5;
247 = 10;
248 = 10;
256 = 10;
262 = 7;
264 = 10;
277 = 10;
282 = 3;
284 = 7;
47 = 5;
75 = 7;
99 = 7;
};
2011 = {
260 = 10;
};
I think core-plot needs an array doesn't it? How would you pack this to be the most efficient?

The choice of data structure is completely up to you. You already have the data in a dictionary, so keep that. Implement the following method in your datasource:
-(NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index;
Assuming you have a separate plot for each year, use the plot parameter to select the appropriate year dictionary from the data dictionary. Use the fieldEnum parameter to determine whether the plot is asking for the x or y value and the index parameter to decide which value in the list to return.
For instance (assuming all values in the dictionaries are stored as NSNumber objects and you're using a scatter plot):
-(NSNumber *)numberForPlot:(CPTPlot *)plot
field:(NSUInteger)fieldEnum
recordIndex:(NSUInteger)index
{
NSDictionary *year = // retrieve the year dictionary based on the plot parameter
NSDictionary *yearData = [year objectAtIndex:index];
NSNumber *num = nil;
switch ( fieldEnum ) {
case CPTScatterPlotFieldX:
num = [yearData objectForKey:#"dayofyear"];
break;
case CPTScatterPlotFieldY:
num = [yearData objectForKey:#"value"];
break;
default:
break;
}
return num;
}

I have actually changed the structure to this.
A dictionary with the year as the key and an array for each point containing the dayofyear and the value.
2009 = (
(354,5),
(338,3),
(234,20),
(298,7),
(192,25)
)
That way the implementation was very easy
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
return [[[self.data objectForKey:plot.identifier] objectAtIndex: index] objectAtIndex: fieldEnum];
}

Related

Octave: How can I index single element of an array?

I have the following code written in Octave:
1 %-----------------------------------------------------------------------------%
2 % INPUT SIGNAL %
3 %-----------------------------------------------------------------------------%
4
5 sampling_frequency = 8000;
6 sampling_period = 1/sampling_frequency;
7 samples_count = 8;
8 time_samples = sampling_period * (0 : samples_count-1);
9
10
11 amplitude_1 = 1;
12 frequency_1 = 1000;
13 phase_shift_1 = 0;
14 signal_samples_1 = amplitude_1*sin(2*pi * frequency_1 * ...
15 time_samples + phase_shift_1);
16
17
18 amplitude_2 = 0.5;
19 frequency_2 = 2000;
20 phase_shift_2 = 3*pi/4;
21 signal_samples_2 = amplitude_2*sin(2*pi * frequency_2 * ...
22 time_samples + phase_shift_2);
23
24 signal_samples = signal_samples_1 + signal_samples_2;
25 for samples_index = 0 : samples_count-1
26 fprintf(stdout, "Sample %d: ", samples_index)
27 endfor
28
As you may see, at line 25, I would like to print value of each sample at separate line, i.e.
Sample <sample_number>: <sample_value>
My question is: how can I index separate member of an array (e.g. input_signal array)?
I have found the solution by myself, so I would like to share it with you.
signal_samples = signal_samples_1 + signal_samples_2;
for samples_index = 1 : samples_count
fprintf(stdout, "Sample %d: %8.5f\n", samples_index-1, ...
signal_samples(samples_index:samples_index))
endfor
Output:
Sample 0: 0.35355
Sample 1: 0.35355
Sample 2: 0.64645
Sample 3: 1.06066
Sample 4: 0.35355
Sample 5: -1.06066
Sample 6: -1.35355
Sample 7: -0.35355

while loop in my program does not fill the second row

I have 2 inner while loops. Code writes the results to first row on the matrix but when it gets to second row, it just passes the other rows and doesn't fill the columns of the rows. How to solve it? The code and output are like below:
while i <= m-1
i
while a <= m-1
a
den1 = sqrt(((xy{i,j}-xy{a+1,b})^2+(xy{i,j+1}-xy{a+1,b+1})^2 ));
dMat(i,a) = den1;
a = a+1;
end
i = i+1;
end
i = 1
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
a = 11
a = 12
a = 13
a = 14
a = 15
a = 16
a = 17
a = 18
a = 19
a = 20
a = 21
a = 22
a = 23
a = 24
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
i = 11
i = 12
i = 13
i = 14
i = 15
i = 16
i = 17
i = 18
i = 19
i = 20
i = 21
i = 22
i = 23
i = 24
You have to restart a to a=1 for each iteration. Put it above the while a <= m-1

How to find connected edges from a list of edges in Matlab

I have a list of edges as follows.
75 77
77 78
78 79
60 63
61 65
65 67
57 62
62 64
67 81
81 85
I want to separate the connected edges as follows.
75 60 61 57
77 63 65 62
78 67 64
79 81
85
I wrote the following code in Matlab.
edges = [75 77; 77 78; 78 79; 60 63; 61 65; 65 67; 57 62; 62 64; 67 81; 81 85];
visited = zeros(size(edges,1),1);
cEdges = [];
cEdgesC = 1;
cEdges(1,cEdgesC) = edges(1,1);
cEdges(2,cEdgesC) = edges(1,2);
visited(1,1) = 1;
orgR = 1;
orgC = 2;
while sum(visited) <= size(visited,1)
cEdgesR = nnz(cEdges(:,cEdgesC));
currentVertex = cEdges(cEdgesR,cEdgesC);
fprintf('\n%d\t%d',cEdgesR,currentVertex);
[foundR,foundC] = find(edges==currentVertex);
foundR(foundR==orgR) = [];
foundC(foundC==orgC) = [];
while isempty(foundR)
fmt=repmat('%d ',1,cEdgesR);
fprintf('\nLoop found: ');
fprintf(fmt,(cEdges(:,cEdgesC))');
cEdgesC = cEdgesC+1;
orgR = find(visited==0, 1, 'first');
orgC = 1;
cEdges(1,cEdgesC) = edges(orgR,orgC);
cEdges(2,cEdgesC) = edges(orgR,orgC+1);
visited(orgR,1) = 1;
cEdgesR = nnz(cEdges(:,cEdgesC));
currentVertex = cEdges(2,cEdgesC);
fprintf('\n%d\t%d',cEdgesR,currentVertex);
[foundR,foundC]=find(edges==currentVertex);
foundR(foundR==orgR) = [];
foundC(foundC==orgC) = [];
if isempty(foundR)
// What to do here?
end
end
fprintf('\t%d\t%d',foundR,foundC);
orgR = foundR;
if foundC == 1
cEdges(cEdgesR+1,1) = edges(foundR,foundC+1);
orgC = foundC+1;
else
cEdges(cEdgesR+1,1) = edges(foundR,foundC-1);
orgC = foundC-1;
end
visited(foundR,1) = 1;
end
The code runs in a loop with no stop. I feel problem is at the end of inner while loop. How may I jump back to the beginning of inner while loop if the same condition is met at the end again? Thank you.
Edit:
Example larger matrix of edges.
If you not consider cyclic cases and backward connection than this should be easy problem.
edges = [75 77; 77 78; 78 79; 60 63; 61 65; 65 67; 57 62; 62 64; 67 81; 81 85];
NumEdges=size(edges,1);
visited = false(NumEdges,1);
List=cell(0);
k=0;
for i=1:NumEdges
if ~visited(i)
k=k+1;
List{k}=edges(i,1);
Idx=i;
while ~isempty(Idx)
visited(Idx)=true;
List{k}(end+1)=edges(Idx,2);
Idx=find(edges(:,1)==edges(Idx,2) & ~visited,1);
end
end
end
celldisp(List)
I solved it including cyclic cases and backward connections as well. Just thought to post for the sake of anyone interested in.
edges = [75 77; 77 78; 78 79; 60 63; 61 65; 65 67; 57 62; 62 64; 67 81; 81 85];
visited = zeros(size(edges,1),1);
cEdges = [];
cEdgesC = 1;
cEdges(1,cEdgesC) = edges(1,1);
cEdges(2,cEdgesC) = edges(1,2);
visited(1,1) = 1;
orgR = 1;
orgC = 2;
cEdgesR = nnz(cEdges(:,cEdgesC));
currentVertex = cEdges(cEdgesR,cEdgesC);
loops = 0;
while sum(visited) < size(visited,1)
i=0;
found=0;
while i<size(edges,1)
i=i+1;
if edges(i,1)==currentVertex && visited(i,1)==0
cEdgesR = nnz(cEdges(:,cEdgesC));
cEdges(cEdgesR,cEdgesC)=edges(i,1);
cEdges(cEdgesR+1,cEdgesC)=edges(i,2);
currentVertex = cEdges(cEdgesR+1,cEdgesC);
visited(i,1)=1;
i=0;
found=1;
elseif edges(i,2)==currentVertex && visited(i,1)==0
cEdgesR = nnz(cEdges(:,cEdgesC));
cEdges(cEdgesR,cEdgesC)=edges(i,2);
cEdges(cEdgesR+1,cEdgesC)=edges(i,1);
currentVertex = cEdges(cEdgesR+1,cEdgesC);
visited(i,1)=1;
i=0;
found=1;
end
end
if found==0
fprintf('\nloop: ');
loops = loops+1;
loopVs = nnz(cEdges(:,loops));
for j=1:loopVs
fprintf('\t%d',cEdges(j,loops));
end
cEdgesT = cEdges.';
cEdgesC = nnz(cEdgesT(:,1))+1;
cEdgesR = 1;
nextEi = find(visited==0, 1, 'first');
cEdges(1,cEdgesC) = edges(nextEi,1);
cEdges(2,cEdgesC) = edges(nextEi,2);
visited(nextEi,1) = 1;
currentVertex = cEdges(2,cEdgesC);
end
end
fprintf('\nno. of loops = %d',loops);

Neural network time series app processes values out of range of data

I want to customize my neural network training to process within the range of [0,65] - I tried the code:
net.inputs{1}.processParams;
ymin = 0;
ymax = 65;
net.outputs{2}.processParams;
ymin = 0;
ymax = 65;
Yet when I check the Time Series-Response graph it still processes values beyond these two values.
Using:
SampleData:
18
39
51
26
13
9
13
9
13
13
13
4
46
52
52
8
T = tonndata(SampleData,false,false);
trainFcn = 'trainbr';
feedbackDelays = 1:2;
hiddenLayerSize = 400;
net = narnet(feedbackDelays,hiddenLayerSize, 'open', trainFcn);
[x,xi,ai,li] = preparets(net,{},{},T);
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
then....to scale the training processing...
net.inputs{1}.processParams;
ymin = 0;
ymax = 65;
net.outputs{2}.processParams;
ymin = 0;
ymax = 65;

MATLAB bsxfun or vectorization

I have been working on vectorizing my code mostly using bsxfun, but I came across a scenario that I can't quite crack. Here is a small sample of problem. I would like to remove the for loops in this code, but I am having a hard time with the tempEA line.
Index = [2; 3; 4;];
dTime = [25; 26; 27; 28; 25; 26; 27; 28; 27; 28];
dIndex = [3; 3; 3; 2; 1; 3; 2; 4; 4; 2];
aTime = [30; 38; 34; 39; 30; 38; 34; 39; 34; 39];
aIndex = [4; 2; 5; 4; 5; 4; 4; 2; 2; 4];
EA = zeros(numel(Index));
for i = 1:numel(Index)
for j = 1:numel(Index)
tempEA = aTime(Index(i) == dIndex(:,1) & Index(j) == aIndex(:,1));
if i == j
elseif tempEA > 0
EA(i,j) = min(tempEA);
else
EA(i,j) = 50;
end
end
end
The answer should look like this:
EA =
0 50 34
38 0 30
34 50 0
Thanks for help in advance.
This uses bsxfun; no loops. It assumes you don't have NaN's among your aTimevalues.
N = numel(Index);
ii = bsxfun(#eq, dIndex.', Index); %'// selected values according to each i
jj = bsxfun(#eq, aIndex.', Index); %'// selected values according to each j
[ igrid jgrid ] = ndgrid(1:N); %// generate all combinations of i and j
match = double(ii(igrid(:),:) & jj(jgrid(:),:)); %// each row contains the matches for an (i,j) combination
match(~match) = NaN; %// these entries will not be considered when minimizing
result = min(bsxfun(#times, aTime, match.')); %'// minimize according to each row of "match"
result = reshape(result,[N N]);
result(isnan(result)) = 50; %// set NaN to 50
result(result<=0) = 50; %// set nonpositive values to 50
result(1:N+1:end) = 0; %// set diagonal to 0
The line result(result<=0) = 50; is only necessary if your aTime can contain nonpositive values. Can it? Or is your elseif tempEA > 0 just a way of checking that tempEA is not empty?