Moving Window in Matlab - matlab

I'm looking to use Matlab to run through a set of data, 5446100 x 6 called xdata1.
I'm looking it to plot the first 100 data points, and after this to run through each point individually.
To start I have:
for i=1:100
NOC1 = CPMD_PCA(xdata1(99+i,:);
How would I then get this to carry on continuously, other than writing out i=2:101, i=3:102 etc

I am not sure about what CPMD_PCA is doing so I'll show you how to plot a moving windows on a simple matlab plot, on a sample data set.
You'll have to adjust the code to your exact application.
%% // just to create a sample set
x = ( 0:pi/10:150*pi ).' ; %//'
y = cos(x).*cos(3*x)+cos(x./4) ;
xdata = [y , -y/2] ;
%% // adjust moving window size and compute other useful parameters
window_size = 100 ; %// the length (in points) of the moving window
nPts = size(y,1) ; %// number of points in a column
n_windows = nPts-window_size ; %// the total number of windows
%% // Do the initial plot (not moving yet) of the first 100 points
idx = 1:window_size ;
hp = plot( xdata( idx , 1) ) ;
%% // Move the plot by one point at each iteration of the loop
for iwin =1:n_windows
idx = idx + 1 ;
set( hp , 'YData' , xdata(idx,1) )
drawnow %// you can comment that line (try with and without)
pause(0.1) %// adjust the timing to your needs
end

To answer you question directly you can wrap your current code in another loop:
for outer_loop_index = 1 : (size(xdata1,1) - 100)
for inner_loop_index = outer_loop_index : outer_loop_index + 99
NOC1 = CPMD_PCA(xdata1(inner_loop_index,:);
end
end

Related

how to do 3D number-density scatter plot using given data?

I have a cubic box with the size of, lets say 300 in each direction. I have some data (contains X,Y,Z coordinates) which represent the distribution of nearly 1 million data-points within this box. I want to specify a color to their Number density (its an intensive quantity used to describe the degree of concentration of countable objects in this case data-points). In another word, Using color to illustrate which part is more condensed in terms of data-points rather than the other parts. The index for the color-bar in the final image should represent the percentage of data-points specified with that color.
I have tried to do it by dividing the whole space in cubic box to 1 million smaller cube (each cube has a length of 3 in all direction). By counting the number of particles within those cube, I will know how they distributed in the box and the number of existed data-points within. Then I can specify a color to them which I wasn’t successful in counting and specifying. Any suggestion is appreciated.
%reading the files
[FileName,PathName,FilterIndex] = uigetfile('H:\*.txt','MultiSelect','on');
numfiles = size(FileName,2);%('C:\final1.txt');
j=1;
X=linspace(0,300,100);
for ii = 1:numfiles
FileName{ii}
entirefile = fullfile(PathName,FileName{ii});
a = importdata(entirefile);
x = a(:,2);
y = a(:,3);
z = a(:,4);
%% I DON'T KNOW HOW TO CREAT THIS LOOP TO COUNT FOR THE NUMBER OF PARTICLES WITHIN EACH DEFINED CUBE %%
for jj = 2:size(X,2)
%for kk=1:m
if x(:)<X(jj) & y(:)<X(jj) & z(:)<X(jj)
x;
end
%end
end
h=figure(j);
scatter3(x, y, z, 'filled', 'MarkerSize', 20);
cb = colorbar();
cb.Label.String = 'Probability density estimate';
end
I need to get a similar result like the following image. but I need the percentage of data-point specified by each color. Thanks in advance.
Here is a link to a sampled data.
Here is a way to count the 3D density of a point cloud. Although I am affraid the sample data you provided do not yield the same 3D distribution than on your example image.
To count the density, the approach is broken down in several steps:
Calculate a 2D density in the [X,Y] plane: This counts the number of points laying in each (x,y) bin. However, at this stage this number of point incorporates all the Z column for a given bin.
For each non-empty (x,y) bin, calculate the distribution along the Z column. We now have the number of point falling in each (x,y,z) bin. Counting the density/percentage is simply done by dividing each count by the total number of points.
Now for each non-empty (x,y,z) bin, we identify the linear indices of the points belonging to this bin. We then assign the bin value (color, percentage, or any value associated to this bin) to all the identified points.
display the results.
In code, it goes like this:
%% Import sample data
entirefile = '1565015520323.txt' ;
a = importdata(entirefile);
x = a(:,1);
y = a(:,2);
z = a(:,3);
npt = numel(x) ; % Total Number of Points
%% Define domain and grid parameters
nbins = 100 ;
maxDim = 300 ;
binEdges = linspace(0,maxDim,nbins+1) ;
%% Count density
% we start counting density along in the [X,Y] plane (Z axis aglomerated)
[Nz,binEdges,~,binX,binY] = histcounts2(y,x,binEdges,binEdges) ;
% preallocate 3D containers
N3d = zeros(nbins,nbins,nbins) ; % 3D matrix containing the counts
Npc = zeros(nbins,nbins,nbins) ; % 3D matrix containing the percentages
colorpc = zeros(npt,1) ; % 1D vector containing the percentages
% we do not want to loop on every block of the domain because:
% - depending on the grid size there can be many
% - a large number of them can be empty
% So we first find the [X,Y] blocks which are not empty, we'll only loop on
% these blocks.
validbins = find(Nz) ; % find the indices of non-empty blocks
[xbins,ybins] = ind2sub([nbins,nbins],validbins) ; % convert linear indices to 2d indices
nv = numel(xbins) ; % number of block to process
% Now for each [X,Y] block, we get the distribution over a [Z] column and
% assign the results to the full 3D matrices
for k=1:nv
% this block coordinates
xbin = xbins(k) ;
ybin = ybins(k) ;
% find linear indices of the `x` and `y` values which are located into this block
idx = find( binX==xbin & binY==ybin ) ;
% make a subset with the corresponding 'z' value
subZ = z(idx) ;
% find the distribution and assign to 3D matrices
[Nz,~,zbins] = histcounts( subZ , binEdges ) ;
N3d(xbin,ybin,:) = Nz ; % total counts for this block
Npc(xbin,ybin,:) = Nz ./ npt ; % density % for this block
% Now we have to assign this value (color or percentage) to all the points
% which were found in the blocks
vzbins = find(Nz) ;
for kz=1:numel(vzbins)
thisColorpc = Nz(vzbins(kz)) ./ npt * 100 ;
idz = find( zbins==vzbins(kz) ) ;
idx3d = idx(idz) ;
colorpc(idx3d) = thisColorpc ;
end
end
assert( sum(sum(sum(N3d))) == npt ) % double check we counted everything
%% Display final result
h=figure;
hs=scatter3(x, y, z, 3 , colorpc ,'filled' );
xlabel('X'),ylabel('Y'),zlabel('Z')
cb = colorbar ;
cb.Label.String = 'Probability density estimate';
As I said at the beginning, the result is slightly different than your example image. This sample set yields the following distribution:
If you want a way to "double check" that the results are not garbage, you can look at the 2D density results on each axis, and check that it matches the apparent distribution of your points:
%% Verify on 3 axis:
Nz = histcounts2(y,x,binEdges,binEdges) ./ npt *100 ;
Nx = histcounts2(z,y,binEdges,binEdges) ./ npt *100 ;
Ny = histcounts2(x,z,binEdges,binEdges) ./ npt *100 ;
figure
ax1=subplot(1,3,1) ; bz = plotDensity(Nz,ax1) ; xlabel('X'),ylabel('Y') ;
ax2=subplot(1,3,2) ; bx = plotDensity(Nx,ax2) ; xlabel('Y'),ylabel('Z') ;
ax3=subplot(1,3,3) ; by = plotDensity(Ny,ax3) ; xlabel('Z'),ylabel('X') ;
Click on the image to see it larger:
The code for plotDensity.m:
function hp = plotDensity(Ndist,hax)
if nargin<2 ; hax = axes ; end
hp = bar3(Ndist,'Parent',hax) ;
for k = 1:length(hp)
zdata = hp(k).ZData;
hp(k).CData = zdata;
hp(k).FaceColor = 'interp';
end
shading interp

Visualizing matrix values in real time

Suppose I have a 5x5 matrix.
The elements of the matrix change (are refreshed) every second.
I would like to be able to display the matrix (not as a colormap but with the actual values in a grid) in realtime and watch the values in it change as time progresses.
How would I go about doing so in MATLAB?
A combination of clc and disp is the easiest approach (as answered by Tim), here's a "prettier" approach you might fancy, depending on your needs. This is not going to be as quick, but you might find some benefits, such as not having to clear the command window or being able to colour-code and save the figs.
Using dispMatrixInFig (code at the bottom of this answer) you can view the matrix in a figure window (or unique figure windows) at each stage.
Example test code:
fig = figure;
% Loop 10 times, pausing for 1sec each loop, display matrix
for i=1:10
A = rand(5, 5);
dispMatrixInFig(A,fig)
pause(1)
end
Output for one iteration:
Commented function code:
function dispMatrixInFig(A, fig, strstyle, figname)
%% Given a figure "fig" and a matrix "A", the matrix is displayed in the
% figure. If no figure is supplied then a new one is created.
%
% strstyle is optional to specify the string display of each value, for
% details see SPRINTF. Default is 4d.p. Can set to default by passing '' or
% no argument.
%
% figname will appear in the title bar of the figure.
if nargin < 2
fig = figure;
else
clf(fig);
end
if nargin < 3 || strcmp(strstyle, '')
strstyle = '%3.4f';
end
if nargin < 4
figname = '';
end
% Get size of matrix
[m,n] = size(A);
% Turn axes off, set origin to top left
axis off;
axis ij;
set(fig,'DefaultTextFontName','courier', ...
'DefaultTextHorizontalAlignment','left', ...
'DefaultTextVerticalAlignment','bottom', ...
'DefaultTextClipping','on');
fig.Name = figname;
axis([1, m-1, 1, n]);
drawnow
tmp = text(.5,.5,'t');
% height and width of character
ext = get(tmp, 'Extent');
dy = ext(4);
wch = ext(3);
dwc = 2*wch;
dx = 8*wch + dwc;
% set matrix values to fig positions
x = 1;
for i = 1:n
y = 0.5 + dy/2;
for j = 1:m
y = y + 1;
text(x,y,sprintf(strstyle,A(j,i)));
end
x = x + dx;
end
% Tidy up display
axis([1-dwc/2 1+n*dx-dwc/2 1 m+1]);
set(gca, 'YTick', [], 'XTickLabel',[],'Visible','on');
set(gca,'XTick',(1-dwc/2):dx:x);
set(gca,'XGrid','on','GridLineStyle','-');
end
I would have thought you could achieve this with disp:
for i=1:10
A = rand(5, 5);
disp(A);
end
If you mean that you don't want repeated outputs on top of each other in the console, you could include a clc to clear the console before each disp call:
for i=1:10
A = rand(5, 5);
clc;
disp(A);
end
If you want to display your matrix on a figure it is quite easy. Just make a dump matrix and display it. Then use text function to display your matrix on the figure. For example
randMatrix=rand(5);
figure,imagesc(ones(20));axis image;
hold on;text(2,10,num2str(randMatrix))
If you want to do it in a for loop and see the numbers change, try this:
for i=1:100;
randMatrix=rand(5);
figure(1),clf
imagesc(ones(20));axis image;
hold on;text(2,10,num2str(randMatrix));
drawnow;
end

MATLAB: how to plot a matrix as an nxn table?

I have a routine that iteratively changes the structure of a matrix and I would like to animate the process, so that a user can actually see the structure changing.
If my matrix is nxn, I want to display the matrix as an nxn table
e.g.
Then using the 'plot' command I would like to have a figure containing:
(gridlines not necessary)
My actual matrix could be 25x25 / 100x100
This short piece of code
n = 25 ;
A = randi(100, n, n) ;
figure ;
xlim([0 n+2]) ;
ylim([0 n+2]) ;
axis off ;
tt = cell(n,n) ;
for i = 1:n
for j = 1:n
tt{i,j} = text(j,n-i,sprintf('%d',A(i,j))) ;
end
end
for k = 1:100
% Get random coordinates
i = randi(n) ;
j = randi(n) ;
% Get random value
v = randi(100) ;
% Remove & update text
delete(tt{i,j}) ;
tt{i,j} = text(j,n-i,sprintf('%d',v)) ;
% Force figure plot
drawnow() ;
% Wait a bit
pause(0.02) ;
end
prints the matrix on a grid, without the lines. It then randomly finds coordinates and change their value.
I'm not sure adding lines help clarity. If really needed, you can simply add them using plot.
You can adjust the sprintf so that it displays your data nicely.
If number are all on top of each other, you can increase the size of the figure by simply dragging the corner of the window.

Matlab - how to plot monte carlo paths transforming into horizontal histogram in one chart

I want to create a chart which would present all the paths resulting from a simulation and then at the point t=T to transform it into horizontal histogram presenting the frequency of the end results.
Is this possible to do in one chart in Matlab?
I don't think there is a really nice way to do this, but I managed to hack together something which looks similar to what you describe. See below my example with a Monte Carlo simulation of a random walk.
% Setup, create test data
col = [0 0.2 0.741] ; % colour
rng(0) ; % reset random number seed
n = 20 ; % number of bins
Te = 1000 ; % simulation length
T = 600 ; % length of trajectory to plot
X = cumsum(randn(Te,1)) ;
Ugly plot code:
% create histogram based on the end of the sample
[H,C] = hist(X(T+1:Te),n) ;
% new figure
fh = figure(999) ;
clf() ;
% trajectory for the first part of the sample
ax0 = subplot(1,2,1) ;
lh = plot(X(1:T),'Parent',ax0) ;
lh.Color = col ;
% histogram for the second part of the sample
ax1 = subplot(1,2,2) ;
bh = barh(ax1,C,H,1) ;
bh.EdgeColor = col ;
bh.FaceColor = col ;
ax1.XTickLabel = '' ;
ax1.YTickLabel = '' ;
% make both axes have the same YLim property and make sure we don't clip anything
YLim(2) = max(ax0.YLim(2),ax1.YLim(2)) ;
YLim(1) = min(ax0.YLim(1),ax1.YLim(1)) ;
ax1.YLim = YLim ;
linkaxes([ax1,ax0],'y') ;
% bump the axes together
ax0.Position = [0.13 0.11 0.440552147239264 0.815] ;
It's not pretty but it works. Result:

Multiple axis breaks

I'm trying to create a plot on Matlab with multiple axis breaks (so something like the following):
I've tried using things like breakyaxis and breakaxis from the Matlab File Exchange, but those only allow for one break, not multiple.
Is there any way to implement this?
The NaN (Not a Number) values can be an annoying thing but also a convenient one in some case.
When you plot data, Matlab will leave a blank in place of every data point which has no value (the NaN). So the principle is to insert these NaN between your datasets and tell Matlab to plot the whole lot. Matlab will leave a blank automatically everywhere there is a NaN.
Here is an example, since you didn't supply sample data I first have to define 3 short data sets resembling the ones you have in your figure:
%% // sample data sets
yf = #(x) 2*x+40+randi(7,size(x)) ;
x1 = 57:61 ; y1 = yf(x1) ;
x2 = 72:76 ; y2 = yf(x2) ;
x3 = 80:83 ; y3 = yf(x3) ;
This is an edited answer to take into account the breaks in the Y axis. To be able to call global operations on the datasets I have to regroup them into a cell array or a structure. The struture approach would use loops on the different data sets, while the cell array allow the use of cellfun to compact the code. I chose this approach and use cellfun extensively.
So first step is put all your data sets in a cell array
%% // have to group the data sets in a cell array or structure to implement global operations
xc = { x1 ; x2 ; x3 } ;
yc = { y1 ; y2 ; y3 } ;
Now the heavy part:
%// find the maximum vertical span of the datasets and the total span
maxVal = cellfun(#max,yc) ;
minVal = cellfun(#min,yc) ;
maxYspan = max( maxVal-minVal ) ;
totalSpan = max(maxVal)-min(minVal) ;
%// find a sensible Y value to add between the datasets, not too wide but
%// enough to see a break`
yBreakIncrement = round( totalSpan / 10 ) ; %// adjust that if necessary
yTickIncrement = round( maxYspan /5 ) ; %// adjust that if necessary
%% // rebuild the Y datasets
%// value to substract to each data set to bring them together (including the break space)
setSubstract = [0 ; cumsum( (minVal(2:end)-maxVal(1:end-1))- yBreakIncrement ) ] ;
%// get 3 new data sets brought together
Yall = cellfun(#minus , yc , num2cell(setSubstract) , 'uni',0) ;
%// concatenate the data sets, inserting NaN in the middle
Yall = cellfun( #(a,b) cat(2,a,b) , Yall , repmat({NaN},length(yc),1) , 'uni',0) ;
Yall = cat( 2, Yall{:} ) ;
%// remove the last trailing NaN
Yall(end) = [] ;
%% // Build the Y labels
%// generate ticks that covers each interval
Y_tickpos = cellfun(#colon, num2cell(minVal), repmat({yTickIncrement},length(yc),1) , num2cell(maxVal) , 'uni',0) ;
%// generate the Y labels based the real Y values
Y_labels = cellstr( num2str( cat(2, Y_tickpos{:} ).') ) ; %'// ignore this comment
%// now adjust the actual position
Y_tickpos = cellfun(#minus , Y_tickpos , num2cell(setSubstract) , 'uni',0) ;
Y_tickpos = cat( 2, Y_tickpos{:} ) ;
%% // Build the X labels (and axis)
%// create a continuous index for the X axis
X = 1:length(Yall) ;
X_labels = cellstr( num2str( cat(2, xc{:} ).') ) ; %'// generate the X labels based the X values
X_tickpos = X(~isnan(Yall)) ; %// prepare a vector for the label positions
%% // Display
plot(X,Yall) %// plot as usual
%// Set the labels at the chosen positions
set(gca, 'XTick' , X_tickpos , 'XTickLabel' , X_labels )
set(gca, 'YTick' , Y_tickpos , 'YTickLabel' , Y_labels )
That should give you something like:
Hopefully enough to get you started. Try to adapt the principle to your data.