Plot dates matlab - matlab

I've a matrix called datevector containing the year, month, day, hour, minutes, seconds of the timeseries that I would like to plot.
datevector = [...
2009 11 4 11 35 0
2009 11 4 11 36 0
2009 11 4 11 37 0
2009 11 4 11 38 0
2009 11 4 11 39 0
2009 11 4 11 40 0]
To plot my data with respect to this time series I create the array containing the time series
xdate = datenum(datevector);
and then I try to plot my data = [1 2 3 4 5 6]
figure
plot(xdate',data)
datetick('x','yyyy-mm-dd HH:MM:SS')
...well the figure I get is not the one expected...I would like to have a minute resolution as in datavector...can you help me?
Thanks!

Related

MATLAB - Sampling Random values

I want to use randsample to sample values from a matrix, but I want the values sampled to be replaced by zero in the matrix. What do I do/Is there a fuction for this?
I think you don't want to use randsample because you have a given matrix (here M). You can use datasample instead to randomly sample existing data. Then you can use the second output of datasample (here ind) to address the entries in the original matrix M and overwrite them easily.
In the following example operates over the second dimension and takes a selection of columns. If you want a selection of rows, change the third argument of datasample to 1 (this is Matlab's default behaviour when no third argument is given).
% create random data
M = randi(20,4,10)
% randomly sample data
[Y,ind] = datasample(M,4,2)
% write 0 for the sampled data in original matrix
M(:,ind) = 0
This is the result:
M =
20 14 6 18 1 9 4 15 11 11
11 5 9 20 8 19 2 9 3 13
20 20 16 4 1 16 13 11 4 9
15 1 4 12 20 18 4 19 11 13
Y =
18 4 15 14
20 2 9 5
4 13 11 20
12 4 19 1
ind =
4 7 8 2
M =
20 0 6 0 1 9 0 0 11 11
11 0 9 0 8 19 0 0 3 13
20 0 16 0 1 16 0 0 4 9
15 0 4 0 20 18 0 0 11 13
Initialized with rng(4).

Matlab: Cut Vector at missing values and create new vectors

I want to write a Matlab script.
In my example I have a vector A=[1 3 4 5 7 8 9 10 11 13 14 15 16 17 19 20 21]
Now I want to cut the vector automatically at the points where a number is missing(here the numbers 2, 6, 12, 18 are missing).
As a result I want to have the vectors [1] and [3 4 5] and [7 8 9 10 11] and [13 14 15 16 17] and [19 20 21]. So as you can see the new vectors have different lenghts.
I thought about using a for loop, but I am not sure how to write these new vectors.
Thank you for your help :)
One liner with diff, cumsum & accumarray -
out = accumarray(cumsum([0 ; diff(A(:))~=1])+1,A(:),[],#(x) {x})
Sample run -
>> A
A =
1 3 4 5 7 8 9 10 ...
11 13 14 15 16 17 19 20 21
>> celldisp(out)
out{1} =
1
out{2} =
3
4
5
out{3} =
7
8
9
10
11
out{4} =
13
14
15
16
17
out{5} =
19
20
21
This is one approach:
s = [find(diff(A(:).')>1) numel(A)]; %'// detect where consecutive difference exceeds 1
s = [s(1) diff(s)]; %// sizes of groups
result = mat2cell(A(:).', 1, s); %'// split into cells according to those sizes
In your example, this gives
>> celldisp(result)
result{1} =
1
result{2} =
3 4 5
result{3} =
7 8 9 10 11
result{4} =
13 14 15 16 17
result{5} =
19 20 21
Another approach (computes group sizes differently):
s = diff([0 sum(bsxfun(#lt, A(:), setdiff(1:max(A(:).'), A(:).')), 1) numel(A)]);
result = mat2cell(A(:).', 1, s);

Selecting specific rows of a matrix in Matlab [duplicate]

This question already has an answer here:
Extract rows from matrix and make a new matrix in MATLAB
(1 answer)
Closed 10 years ago.
I have a 6639x5 matrix in Matlab and I would like to select certain specific rows in a particular order( say 1st,11th,21st,31st rows... and subsequent additions of 10 until end) to form a new matrix.Any ideas?
Thank you,
Oti.
subset = a(1:10:end, :);
Selects every 10th row until the end, and all columns.
Example:
>> a = magic(5)
a =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> a(1:2:end, :)
ans =
17 24 1 8 15
4 6 13 20 22
11 18 25 2 9

Sorting a vector by the number of time each value occurs

We have the following case:
Q = [idxcell{:,1}];
Sort = sort(Q,'descend')
Sort =
Columns 1 through 13
23 23 22 22 20 19 18 18 18 18 17 17 17
Columns 14 through 26
15 15 14 14 13 13 13 12 12 12 11 10 9
Columns 27 through 39
9 9 8 8 8 8 8 7 7 7 7 7 7
Columns 40 through 52
7 6 6 6 5 4 4 3 3 3 3 2 2
Columns 53 through 64
2 2 2 2 2 2 2 1 1 1 1 1
How can we sort matrix Sort according to how many times its values are repeated?
Awaiting result should be:
repeatedSort = 2(9) 7(7) 1(5) 8(5) 3(4) 18(4) 6(3) 9(3) 12(3) 13(3) 17(3) 4(2) 14(2) 15(2) 22(2) 23(2) 5(1) 10(1) 11(1) 19(1) 20(1)
or
repeatedSort = 2 7 1 8 3 18 6 9 12 13 17 4 14 15 22 23 5 10 11 19 20
Thank you in advance.
You can use the TABULATE function from the Statistics Toolbox, then call SORTROWS to sort by the frequency.
Example:
x = randi(10, [20 1]); %# random values
t = tabulate(x); %# unique values and counts
t = t(find(t(:,2)),1:2); %# get rid of entries with zero count
t = sortrows(t, -2) %# sort according to frequency
the result, where first column are the unique values, second is their count:
t =
2 4 %# value 2 appeared four times
5 4 %# etc...
1 3
8 3
7 2
9 2
4 1
6 1
Here's one way of doing it:
d = randi(10,1,30); %Some fake data
n = histc(d,1:10);
[y,ii] = sort(n,'descend');
disp(ii) % ii is now sorted according to frequency

How can I visualize/plot temperature gradient using matlab?

I have data at specific points in 3D rectangle and I want to see temperature gradient. I have values at specific points , but I want a continous flow of gradient between each sensor. I am not been able to figure out how to visualize or map data in between each sensors placed at different points. stucked :(
X=[5 0 0 0 0 5 10 10 10 10 0 5 10 10 0 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 5 10 0 5 10 10 10 5 0 0]';
Y=[10 10 5 5 10 10 5 10 5 10 0 0 0 0 0 0 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 0 0 0 0 0 0 5 10 10 10 5 ]';
Z=[20 20 20 14 14 14 14 14 20 20 20 20 20 14 14 14 3.8 3.8 0 0 7.5 7.5 10 10 12.5 12.5 15 15 17.5 17.5 20 20 0 0 0 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5]';
%# temperature vector
T = [20 22 24 22.1 26.1 22.4 15 17 21 22 19 22 18 17 18 20 21 22 21 24 22.3 22.5 22.8 28.9 22 27 26 20 19 24 21 23 19 18 22 25 27 21 29 25 22 21 22];
scatter3(X,Y,Z,[4000],T,'.');
grid off
box off
view(32,18); axis equal tight off vis3d; % azimuth 26
camproj perspective
camlight; lighting gouraud; alpha(0.75);
rotate3d on
Code below just shows how my one side of 3d rectangle should look like(its just a random code)
datagrid = 500*peaks(100);
R = makerefmat('RasterSize',size(datagrid));
[aspect,slope,gradN,gradE] = gradientm(datagrid,R);
figure; axesm eqacyl
meshm(datagrid,R)
colormap (jet(64))
colorbar('vert')
title('Peaks: elevation')
axis square
You can break the problem down into two sub-problems:
Interpolation
Visualization
Let's take a look at interpolation first. There are many methods available but let's try the MATLAB function griddatan. This will interpolate (linearly) values onto a new set of points (here I've used a regular grid constructed using meshgrid).
M = 20;
N = 20;
L = 40;
T = transpose(T);
% Set up the grid of points we wish to interpolate at
[xi,yi,zi] = meshgrid(linspace(0,10,M),linspace(0,10,N),linspace(0,20,L));
% Perform interpolation
ti = griddatan([X,Y,Z],T,[xi(:),yi(:),zi(:)]);
% Reshape back from a column vector to a MxNxL matrix
ti = reshape(ti, size(xi));
% Visualise in some way
scatter3(xi(:),yi(:),zi(:),400,ti(:),'.')
When it comes to visualization then the sky's the limit and 3D volume visualization is more of an art than a science. I'm afraid I can't run your example (I don't have access to makerefmat) but http://www.mathworks.co.uk/help/techdoc/visualize/bqliccy.html has some good starting points.