First convert it into a list, by using list() function. Then use the count method to count.
list(nda).count(1)
MATLAB applications, tutorials, examples, tricks, resources,...and a little bit of everything I learned ...
Showing posts with label array. Show all posts
Showing posts with label array. Show all posts
Wednesday, September 23, 2015
Friday, February 22, 2013
Convert a dataset to matrix
For the simplest case when the dataset only contains numbers, this double function can be used to convert a dataset to matrix, or a vector if there is only one variable in the dataset:
m=double(ds)
m=double(ds)
Monday, November 28, 2011
remove any rows that contains a specific number in Matlab
In this post, I am trying to solve the problem given in the comments of one of the old post.
Here's the problem, if I understand it correctly:
a =
1 2 3 4 5
2 3 4 -99 6
3 4 5 6 7
4 -99 6 7 8
5 6 7 8 9
How to remove all the rows that contains the number -99, in this case, the row #2 and #4?
And here's the code I wrote to do this job:
[rows col]=size(a); %count the number of rows and columns of matrix a
j=0; %initialize a counter;
for i=1: rows
if sum(a(i,:)~=-99)==col; %This determines if the row has -99 or not, if not, do nothing
else %shift all of the rest of the rows up one row
for m=i: rows-1;
a(m,:)=a(m+1,:);
end
j=j+1; %count how many rows has -99, which equals how many times the rows has been shifted up
end
end
b=a((1:rows-j),:); %get rid of the last j rows
The result is:
b =
1 2 3 4 5
3 4 5 6 7
5 6 7 8 9
The result is:
b =
1 2 3 4 5
3 4 5 6 7
5 6 7 8 9
I hope this can work for you, Winifred. Thanks for your comments!
Friday, July 1, 2011
Cell array, self-defined function, and save fig
I learned a lot of important things yesterday, after spend 10 hours in front of computer playing with MatLab. Alright, here's the code I came up with.
%Created on 6/30/2011
clear all
clc
sitenumber='01';
SampleLocation=[];
AER=[];
Err=[];
%% Importing data from intermediate file
filename='whatever it is.xlsx';
D1=xlsread(filename, 'A14:C25');
D2=xlsread(filename, 'I14:K25');
D3=xlsread(filename, 'Q14:S25');
D4=xlsread(filename, 'Y14:AA25');
D5=xlsread(filename, 'AG14:AI25');
D6=xlsread(filename, 'AO14:AQ25');
D7=xlsread(filename, 'AW14:AY25');
D8=xlsread(filename, 'BE14:BG25');
D9=xlsread(filename, 'BM14:BO25');
D10=xlsread(filename, 'BU14:BW25');
D11=xlsread(filename, 'CA14:CD25');
D12=xlsread(filename, 'CJ14:CL25');
D13=xlsread(filename, 'CR14:CT25');
D14=xlsread(filename, 'CZ14:DB25');
D15=xlsread(filename, 'DH14:DJ25');
D16=xlsread(filename, 'DP14:DR25');
D17=xlsread(filename, 'DX14:DZ25');
D18=xlsread(filename, 'EF14:EH25');
DCell={D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,...
D11,D12,D13,D14,D15,D16,D17,D18}; % Put all the arrays into one big cell array so that each time I can take one cell out and use it as the function input.
% Use each array as function input % This is a self-defined function
for k=1:18
[AERate Error]=getDecayrate(DCell{k}); %If use DCell (k), the function won't work.
SampleLocation(1,k)=k;
AER(1,k)=AERate;
Err(1,k)=Error;
newfilename=strcat('Site_', sitenumber, '_SampleLocation_', num2str(k),'.fig');
saveas(gcf,newfilename) % This saveas function works pretty good. The .fig file can be edited later in MatLab.
end
Result=[SampleLocation', AER', Err'];
%The following function calculates the first-order decay rate of the tracer gas, and returns two parameters.
function [AER Err] = getDecayrate( location)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%AER=Data-1;
TimeS=zeros(1, length(location))';
LNC1=zeros(1, length(location))';
LNC2=zeros(1, length(location))';
for i=2 : length(location)
TimeS(i)=(location(i,1)-location(1,1))*1440/60;
end
for j=2 : length(location)
LNC1(j)=log(location(j,2)/location(1,2));
LNC2(j)=log(location(j,3)/location(1,3));
end
slope1=sum(TimeS.*LNC1)/sum(TimeS.^2);
slope2=sum(TimeS.*LNC2)/sum(TimeS.^2);
AER=abs((slope1+slope2)/2);
Err=abs(AER-abs(slope1));
TimeModel=0:0.01:max(TimeS);
LNC1Model=TimeModel.*slope1;
LNC2Model=TimeModel.*slope2;
plot(TimeS, LNC1, 'xr',TimeS, LNC2, 'sb', TimeModel, LNC1Model,'r', TimeModel, LNC2Model, 'b', 'MarkerSize',10, 'LineWidth', 2)
title ('Location')
xlabel('Time (hours)')
ylabel('Ln(C/C0)')
end
%Created on 6/30/2011
clear all
clc
sitenumber='01';
SampleLocation=[];
AER=[];
Err=[];
%% Importing data from intermediate file
filename='whatever it is.xlsx';
D1=xlsread(filename, 'A14:C25');
D2=xlsread(filename, 'I14:K25');
D3=xlsread(filename, 'Q14:S25');
D4=xlsread(filename, 'Y14:AA25');
D5=xlsread(filename, 'AG14:AI25');
D6=xlsread(filename, 'AO14:AQ25');
D7=xlsread(filename, 'AW14:AY25');
D8=xlsread(filename, 'BE14:BG25');
D9=xlsread(filename, 'BM14:BO25');
D10=xlsread(filename, 'BU14:BW25');
D11=xlsread(filename, 'CA14:CD25');
D12=xlsread(filename, 'CJ14:CL25');
D13=xlsread(filename, 'CR14:CT25');
D14=xlsread(filename, 'CZ14:DB25');
D15=xlsread(filename, 'DH14:DJ25');
D16=xlsread(filename, 'DP14:DR25');
D17=xlsread(filename, 'DX14:DZ25');
D18=xlsread(filename, 'EF14:EH25');
DCell={D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,...
D11,D12,D13,D14,D15,D16,D17,D18}; % Put all the arrays into one big cell array so that each time I can take one cell out and use it as the function input.
% Use each array as function input % This is a self-defined function
for k=1:18
[AERate Error]=getDecayrate(DCell{k}); %If use DCell (k), the function won't work.
SampleLocation(1,k)=k;
AER(1,k)=AERate;
Err(1,k)=Error;
newfilename=strcat('Site_', sitenumber, '_SampleLocation_', num2str(k),'.fig');
saveas(gcf,newfilename) % This saveas function works pretty good. The .fig file can be edited later in MatLab.
end
Result=[SampleLocation', AER', Err'];
%The following function calculates the first-order decay rate of the tracer gas, and returns two parameters.
function [AER Err] = getDecayrate( location)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%AER=Data-1;
TimeS=zeros(1, length(location))';
LNC1=zeros(1, length(location))';
LNC2=zeros(1, length(location))';
for i=2 : length(location)
TimeS(i)=(location(i,1)-location(1,1))*1440/60;
end
for j=2 : length(location)
LNC1(j)=log(location(j,2)/location(1,2));
LNC2(j)=log(location(j,3)/location(1,3));
end
slope1=sum(TimeS.*LNC1)/sum(TimeS.^2);
slope2=sum(TimeS.*LNC2)/sum(TimeS.^2);
AER=abs((slope1+slope2)/2);
Err=abs(AER-abs(slope1));
TimeModel=0:0.01:max(TimeS);
LNC1Model=TimeModel.*slope1;
LNC2Model=TimeModel.*slope2;
plot(TimeS, LNC1, 'xr',TimeS, LNC2, 'sb', TimeModel, LNC1Model,'r', TimeModel, LNC2Model, 'b', 'MarkerSize',10, 'LineWidth', 2)
title ('Location')
xlabel('Time (hours)')
ylabel('Ln(C/C0)')
end
Wednesday, June 1, 2011
simple data matching by using dataset arrays
Task : There are two Excel files, one contains the sample number and the time of sampling while the other contains the analysis results of each sample. Because the samples were taken in a chronicle order and were analysed in random order, it is kind of time consuming to manually find out each sample's result and put them into one file, especially when dealing with thousands of samples.
Here's a short code I wrote to do this:
% This program search the bag numbers and put the corresponding
% concentration values into the data file.
%% initialization
clear all;
clc;
%% import data
dataf=dataset('xlsfile','back.xls'); %aisle files from field test
concf=dataset('xlsfile','concentration.xls'); %concentration readings from autoTrac
%% determine dataset array size
datafL=length(dataf);
concfL=length(concf);
%% search and match
for i=1 : datafL
for j= 1 : concfL-1
if ( char(dataf.BagNumber(i))==char(concf.BagNumber(j)) )
dataf.Concentration1(i)=concf.Concentration(j);
dataf.Concentration2(i)=concf.Concentration(j+1);
break
end
end
end
%% Output data
export(dataf, 'xlsfile', 'Processed bag files-back.xls')
The dataset array is very power at handling different type of data, including numbers, strings, and even matrix.
To write a dataset array to an Excel file, I need to use export function instead of xlswrite.
Here's a short code I wrote to do this:
% This program search the bag numbers and put the corresponding
% concentration values into the data file.
%% initialization
clear all;
clc;
%% import data
dataf=dataset('xlsfile','back.xls'); %aisle files from field test
concf=dataset('xlsfile','concentration.xls'); %concentration readings from autoTrac
%% determine dataset array size
datafL=length(dataf);
concfL=length(concf);
%% search and match
for i=1 : datafL
for j= 1 : concfL-1
if ( char(dataf.BagNumber(i))==char(concf.BagNumber(j)) )
dataf.Concentration1(i)=concf.Concentration(j);
dataf.Concentration2(i)=concf.Concentration(j+1);
break
end
end
end
%% Output data
export(dataf, 'xlsfile', 'Processed bag files-back.xls')
The dataset array is very power at handling different type of data, including numbers, strings, and even matrix.
To write a dataset array to an Excel file, I need to use export function instead of xlswrite.
Subscribe to:
Posts (Atom)
my-alpine and docker-compose.yml
``` version: '1' services: man: build: . image: my-alpine:latest ``` Dockerfile: ``` FROM alpine:latest ENV PYTH...
-
It took me a while to figure out how to insert a space in Mathtype equations. This is especially useful when you write an equation with mult...
-
In this post, I am trying to solve the problem given in the comments of one of the old post. Here's the problem, if I understand it co...
-
Recently I got a very long column of data and it contains lots of NaN. I found the finite function very useful to help me remove all the NaN...