I do not want to plot the 0 components of my vector; how do I remove these components without effecting the length of the vector?
38 views (last 30 days)
Show older comments
I want to plot energy as a function of time. Using the plot function this is simple; however my graph diverges to 0 due to the 0 elements in my energy-vector. How do I remove these zeros without effecting the length of the vector?
I have tried using the following function, however this simply eliminates the zeros and causes one vector to be shorter than the other, which does not allow me to create a plot:
Energy(Energy==0) = [];
I am relatively new to Matlab so perhaps I am missing something simple. I have also attached an example of the type of plot I am getting which you will find clearly jumps to zero.
Thanks in advance!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146813/image.jpeg)
0 Comments
Accepted Answer
dpb
on 15 Jan 2015
Edited: dpb
on 16 Jan 2015
For plotting, use
Energy(Energy==0) = nan;
instead. plot and friends ignore NaN entries altho if you're doing calculations on the values they will propagate thru those so you'll have to deal with them.
The alternate way if the zero values are really of no consequence is to fixup both arrays
ix=(Energy==0); % get the logical vector of zero locations
Energy(ix)=[]; % remove those from that array
time(ix)=[]; % and from the time vector, too.
Or, yet another workaround
ix=(Energy~=0); % this time keep the desired indices
plot(t(ix),Energy(ix)) % plot only those particular ones
This latter is also how to do computations on just the selected portion but not actually modify the underlying data arrays.
More Answers (1)
Zoltán Csáti
on 15 Jan 2015
Use
Energy(Energy == 0) = NaN;
It will not shorten the length of the vector, but will not be displayed on the graph.
2 Comments
Ghansyam Vadodaria
on 29 Jan 2020
Edited: dpb
on 29 Jan 2020
hello,
i want to draw graph of store data on dat file but in this data some zeros are available. i want to continue my graph but dont want discontinuety, and also plot smooth graph. but discontinuty occure how can i solve this??
please follow the given programm and figure..
ackley=importdata('E:\codeblock\9\Ackley_error\student.dat');
Y_Axis =length(ackley);
Y_Axis = Y_Axis + 1;
Aplot=ackley;
Aplot(Aplot==0)=NaN; % replace 0 elements with NaN
t=1:length(ackley);
semilogy(t,ackley,'-g','LineWidth',2,'LineStyle','-');
hold on
legend('Aplot','student');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/268981/image.png)
dpb
on 29 Jan 2020
As noted previously, NaN elements are simply ignored by plot() and friends resulting in "holes" in a drawn line. If this isn't what you want, then the alternative is as also shown in my response above is to only select the nonzero elements.
In the code you posted above, the lines
Aplot=ackley;
Aplot(Aplot==0)=NaN; % replace 0 elements with NaN
are superfluous as you never use the Aplot variable. For a semilogy() plot, the effect in the end is the same as one can't take the log() of zero so semilogy ignores those points as well.
There is no way to plot zeros on logarithmic scale so the choice is to either make them very, very small but positive in which case they'll show up on the plot as elements that won't be on the path of the other data you do have or select only the data for which y ~= 0 and plot it instead.
From my above Answer
ix=(Energy~=0); % this time keep the desired indices
plot(t(ix),Energy(ix)) % plot only those particular ones
following the same pattern w/ your variables and for a semilogy plot would be
...
ix=(ackley~=0); % this time keep the desired indices
semilogy(t(ix),ackley(ix),'-g','LineWidth',2,'LineStyle','-');
...
You don't show what created the line for student so didn't try to reproduce it but similar logic holds.
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!