matlab plot jump discontinuity [duplicate] - matlab

This question already has an answer here:
Plotting a Continuous signal
(1 answer)
Closed 8 years ago.
Is it possible to plot a sequence of data without interpolating them?
I would like to keep the value of one data point until the value of the next one.
So for example:
I have my time vector
time = [1 2 3 4 5 6]
and datapoints =[3 0 1 4 5 6]
I would like that the line of my plot is 3 from time(1) till time(2) and then it should change in time(2) to take the value 0.
Basically in correspondence of the time 2 my plot should assume both the value 3 and 0 (jump discontinuity)...
I hope I was clear.

A short answer: yes you can.
Use the stairs() function.
time=[1 2 3 4 5 6];
datapoints = [3 0 1 4 5 6];
stairs(time,datapoints)
Good luck!

you have to duplicate the times as well the datapoints and plot them shifted in order to get the staircase-style:
time = [1 2 3 4 5 6];
datapoints =[3 0 1 4 5 6];
datapoints = [datapoints, datapoints];
[time, idx] = sort([time, time]);
time(1) = [];
datapoints = datapoints(idx);
datapoints(end) = [];
plot(time,datapoints)

Related

How to split vector by zeros in MATLAB

I have got a problem with splitting a vector by zeros.
I have a vector for example
v=[1 3 2 6 4 0 0 2 4 6 0 0 0 3 1]
I need to get vectors like
v1=[1 3 2 6 4]
v2=[2 4 6]
v3=[3 1]
Is there any way to do this by using MATLAB functions?
Of course I don't know of how many subvectors are included in main vector v and how many zeros delimits vectors.
I'm not a programmer and also I'm not a pro in MATLAB.
I know a procedural way to do this but want do it by MATLAB somehow.
I found a function A = strsplit(str,delimiter) but I don't have string I have a vector.
So I searched for conversion function. I found S = char(V) but when I executed it it crashed.
It's better to have the output as a cell array, not as separate variables. That way the output will be easier to handle.
Try this:
v = [1 3 2 6 4 0 0 2 4 6 0 0 0 3 1]; %// data
w = [false v~=0 false]; %// "close" v with zeros, and transform to logical
starts = find(w(2:end) & ~w(1:end-1)); %// find starts of runs of non-zeros
ends = find(~w(2:end) & w(1:end-1))-1; %// find ends of runs of non-zeros
result = arrayfun(#(s,e) v(s:e), starts, ends, 'uniformout', false); %// build result
Result (for your example):
>> result{:}
ans =
1 3 2 6 4
ans =
2 4 6
ans =
3 1
The strsplit() solution for a vector of whole numbers smaller than 9 (so a very specific solution, for a general solution see Luis Mendo's). Split and convert back to number:
res = strsplit(char(v), char(0));
res = cellfun(#(x) x - 0,res,'un',0);
celldisp(res)
res{1} =
1 3 2 6 4
res{2} =
2 4 6
res{3} =
3 1

plot elements of array with different colors

I have a vector of integers that vary from 1 to 4.
A=[1 2 3 4 2 3 2 1 2 3 4 4]
I would like to plot A with different colors for each value...
the vertical line that links A(1) to A(2) should have the color of the first value (in this case 1).
Is that possible?
and how to handle the case of NaN present in the vector?
I should plot A against a time vector
A = [1 1 1 NaN 4 4 4 Nan 2 2 3 3];
time = [1 2 3 4 5 6 7 8 9 10 11 12];
Suppose you have the following set of colors:
col = hsv(4);
You set the order of the colors based on the values of A:
figure();
set(gca, 'ColorOrder', col(A,:), 'NextPlot', 'replacechildren');
Then, you can plot each line in the desired color:
n = numel(A);
plot(hankel(0:1,1:n-1),hankel(A(1:2),A(2:n)))
This results in:
Edit:
The hankel approach is a bit like shooting with a bazooka to kill a mosquito, as we say in the Netherlands. Anyway, I learned about it a few questions ago - so I liked to use it. See the post of Dan for a simpler alternative for the plotting. Still, setting the correct colors can be done as in the above.
You can do it using just a touch of trickery:
A=[1 2 3 4 2 3 2 1 2 3 4 4]
x = [1:numel(A)-1; 2:numel(A)];
y = A(x);
plot(x,y)

Matlab, How to get each column in a matrix

I got a 4-by-n matrix, like
A =
1 5 9
3 0 6
2 3 10
7 8 4
What I want to do with A is getting each half column of A as
Line1Point1 = [1 3]
Line1Point2 = [2 7]
Line2Point1 = [5 0]
Line2Point2 = [3 8]
Line3Point1 = [9 6]
Line3Point2 = [10 4]
How could I do that? I’m pretty new to matlab coding.. Any help is really appreciated..
Cheers
Use reshape function, for example:
>> A = [1 5 9;
3 0 6;
2 3 10;
7 8 4];
>> reshape(A,2,6)
ans =
1 2 5 3 9 10
3 7 0 8 6 4
Storing such information as many variables is generally a bad idea
Some options for storing and accessing are
Cell array
Line=mat2cell(A,[2,2],ones(1,size(A,2))).'
access with
Line{2,1}
ans =
5
0
Indexing
as other answers
Anonymous Function
Line=#(l,p)A(2*p-1:2*p,l)
access with
Line(2,1)
ans =
5
0
Structure
Not really a useful solution, more for interests sake
for ii=1:size(A,2);for jj=1:2;Line(ii).Point(jj).Value=A(2*jj-1:2*jj,ii);end;end
access with
Line(2).Point(1).Value
ans =
5
0
A(1:2,1) will give you first half of the first column.
A(3:4,1) will give you second half of the first column.
A(1:2,2) will give you first half of the second column.
A(3:4,2) will give you second half of the second column.
A(1:2,3) will give you first half of the third column.
A(3:4,3) will give you second half of the third column.
You can create the variables with the eval function, which executes the input string. Using eval is commonly regarded as bad practice since it is horrible to debug.
Nevertheless, here's the code:
A = [1 5 9; 3 0 6; 2 3 10; 7 8 4];
for ii = 1:length(A(1,:))
eval(['Line' num2str(ii) 'Point1 = A(1:2, ii)' ]);
eval(['Line' num2str(ii) 'Point2 = A(3:4, ii)' ]);
end
% Now all variables are created - for example: Line2Point1
A more elegant solution could be to store the vectors in a cell array. You can acces the first vectors for example by typing: c{1,1}
c = cell(length(A(1,:)),2)
for ii = 1:length(A(1,:))
c{ii,1} = A(1:2, ii);
c{ii,2} = A(3:4, ii);
end
I would suggest using 3D arrays to store and then access those values.
Code
N = size(A,1)/2;
LinePoint = permute(reshape(A,N,size(A,1)/N,[]),[1 3 2])
Here,
2nd dimension indices (columns) would represent Line IDs
3rd dimension indices would represent Point IDs.
Thus, the representative 3D array would be - LinePoint(:,LineID,PointID).
Example run
For your given A, we would have LinePoint as -
LinePoint(:,:,1) =
1 5 9
3 0 6
LinePoint(:,:,2) =
2 3 10
7 8 4
Thus,
Line1Point1 would be denoted by LinePoint(:,1,1)
Line1Point2 would be denoted by LinePoint(:,1,2)
Line2Point1 would be denoted by LinePoint(:,2,1)
Line2Point2 would be denoted by LinePoint(:,2,2)
Line3Point1 would be denoted by LinePoint(:,3,1)
Line3Point2 would be denoted by LinePoint(:,3,2)

Getting the cumsum of vector with the first element counting itself as well

I would like to get the cumsum of an vector, but need the first element of the vector to count itself as well. An example:
a = [1 2 3 4 5]
and the result needs to look as follow:
2 3 6 10 15
Presumably you already know about the cumsum function so, have you not tried?:
a = [1 2 3 4 5];
s = cumsum(a);
s(1) = s(1)+a(1)
which returns
s =
2 3 6 10 15

Verify a matrix value on MATLAB 2 [duplicate]

This question already has answers here:
Verify a matrix value on MATLAB
(3 answers)
Closed 8 years ago.
I am using MATLAB. I have a question about how i can verify that the values of a matrix are being repeating, like this:
A=[ 2 3 2 3 2 3 2 3]
with the answer AUX=1
If the matrix A repeat at least the first two values for all columns after, i want a AUX = 1. but if not, only AUX= 0.
or
A=[ 2 3 3 2 2 3 3 2]
with the answer AUX=1
If the matrix A repeat like before, i want a AUX = 1. but if not, only AUX= 0.
The matrix A can also have zeros numbers after the numbers. (example, A = [ 1 2 1 2 1 0 0 0], A = [ 2 3 3 2 2 3 3 2 0 0 0 ].)
I think you are looking for this, finding whether the same two numbers are used in each non overlapping window of 2 values:
% Cutting off the tail
x = [1 2 2 1 1 2 1 2 0 0];
x = x(1:find(x,1,'last'));
x = x(1:2*fix(numel(x)/2));
% Checking for allowed values (the first 2 values, each one once)
M=sort(reshape(x,2,[]));
AUX = size(unique(M','rows'),1)==1
Note that this cuts of trailing zeros, so you may need an extra step if the number of remaining elements is not always odd but that should be easy.
% A
A = [1 2 3 4 4 5; 1 3 4 5 6 5; 6 7 4 1 3 3];
% make A a column vector
A_col = A(:);
% calculate histogram of A with max(A_col) bins
n = hist(A_col, max(A_col));
AUX = sum(n > 1) > 0