Thursday, July 23, 2009

Matlab: Matrix to a single column

Example:

a=
1 2 3
4 5 6
7 8 9

a=a(:)
->
a=
1
4
7
2
5
8
3
6
9

Matlab: Remove all the unwatned elements in matrix

Today I just got a big set of data, the record of ozone level for one year. When the data is bad or there is no data for a certain point of time , they just use a 999. So I want to remover all of the 999s in this matrix.

If use Excel, that's easy- Find and Replace. In matlab, what I did is this:

Matrix=Matrix(Matrix ~=999)

Here's a simpler example:

a=[1 2 3 4 5 6 7 8 9 ];
b=a(a~=4);
->
b=[1 2 3 5 6 7 8 9]

Wednesday, July 15, 2009

Casio FX4500PA Calculator User Manual Download

I got a Casio fx4500PA calculator several years ago, but I just use it for the simplest calculation. And today I realized that it might be interesting if I do some more complicated job on this calculator. So I found this user manual and it can tell me how to do INTEGRATION and other programming on it. It's pretty cool.

Here's the link to download the user manual:

Casio FX4500PA Calculator User Manual Download

Wednesday, July 1, 2009

Calculate Sequence and Sum

For a(1)=sqrt(2) and a(n+1)=sqrt(2+sqrt(n)), calculate the sequence and sum for n=10.

clc; clear all; close all;

format long;

a=[];


S=[];


a(1)=sqrt(2);


for k=2:11


a(k)=sqrt(2+a(k-1))


end


a=a';


for m=1:k


S(m)=sum(a(1:m));


end



Tuesday, June 30, 2009

3rd Movie

Well, this time I wrote the code by myself.

clc; clear all; close all;
Fig=figure;
aviobj=avifile('sphere.avi');

for k=1:6
n=2^k-1;
[x y z]=sphere(n);
c = hadamard(2^k);
surf(x, y, z, c)
colormap([1 1 0; 0 1 1]);
axis off; axis equal;
F=getframe(Fig);
for m=1:15
aviobj=addframe(aviobj, F);
end
end
for k=5:-1:1
n=2^k-1;
[x y z]=sphere(n);
c = hadamard(2^k);
surf(x, y, z, c)
colormap([1 1 0; 0 1 1]);
axis off; axis equal;
F=getframe(Fig);
for m=1:15
aviobj=addframe(aviobj, F);
end
end
close(Fig)
aviobj = close(aviobj);


Another avi 'Movie' by avifile function

Saturday, June 27, 2009

Pictures of magic matrix (50x50)

Plotmatrix: This one is 5x5
area:

bar:

comet:

compass:

contour:

contourf:

image:

imagesc:

loglog:

mesh:

pcolor:

plot:

polar:

ribbon:

semilogx

semilogy

spy

stairs

stem3

stem

surf

Friday, June 26, 2009

avi 'Movie' created by avifile function

The code used to create this 'movie':


t = linspace(0,2.5*pi,40);
fact = 10*sin(t);fig=figure;aviobj = avifile('example.avi')
[x,y,z] = peaks;
for k=1:length(fact) 
h = surf(x,y,fact(k)*z);
axis([-3 3 -3 3 -80 80]) 
axis off 
caxis([-90 90]) 
F = getframe(fig);
aviobj = addframe(aviobj,F);
endclose(fig)aviobj = close(aviobj);
 
The code above was copied from HERE .


Sunday, June 21, 2009

Tracking Code


Matlab: Remove the NaN elements

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 elements. Also it can remove the Inf elements. For example:
C1=[0 1 2 3 NaN 4 5 6 NaN 7 Inf 8 Inf 9];
C2=C1(finite(C1));

we get :
C2=[0 1 2 3 4 5 6 7 8 9]

I'm using R2008a version and I got a warning like this:
Warning: FINITE is obsolete and will be removed in future versions. Use ISFINITE instead.

Thursday, June 18, 2009

xlsread

Today I wrote a small piece to read data from xls files. Each file has several sheets and I only need one single column from each sheet. And at the end, I concatenated the columns to make a very very long colmn.

clc; clear all; close all;
O51=xlsread('TX2005.xls',1,'E1:E8760');
O52=xlsread('TX2005.xls',2,'E1:E8760');
O53=xlsread('TX2005.xls',3,'E1:E8760');
O54=xlsread('TX2005.xls',4,'E1:E8760');
O55=xlsread('TX2005.xls',5,'E1:E8760');
% O56=xlsread('NJ2005.xls',6,'E1:E8760');
% O57=xlsread('NJ2005.xls',7,'E1:E8760');
% O58=xlsread('NJ2005.xls',8,'E1:E8760');
O61=xlsread('TX2006.xls',1,'E1:E8760');
O62=xlsread('TX2006.xls',2,'E1:E8760');
O63=xlsread('TX2006.xls',3,'E1:E8760');
O64=xlsread('TX2006.xls',4,'E1:E8760');
O65=xlsread('TX2006.xls',5,'E1:E8760');
O66=xlsread('TX2006.xls',6,'E1:E8760');
O67=xlsread('TX2006.xls',7,'E1:E8760');
O68=xlsread('TX2006.xls',7,'E1:E8760');
O69=xlsread('TX2006.xls',7,'E1:E8760');
O71=xlsread('TX2007.xls',1,'E1:E8760');
O72=xlsread('TX2007.xls',2,'E1:E8760');
O73=xlsread('TX2007.xls',3,'E1:E8760');
O74=xlsread('TX2007.xls',4,'E1:E8760');
O75=xlsread('TX2007.xls',5,'E1:E8760');
O76=xlsread('TX2007.xls',6,'E1:E8760');
O77=xlsread('TX2007.xls',7,'E1:E8760');
O78=xlsread('TX2007.xls',4,'E1:E8760');
O79=xlsread('TX2007.xls',5,'E1:E8760');
O710=xlsread('TX2007.xls',6,'E1:E8760');
O711=xlsread('TX2007.xls',7,'E1:E8760');
O712=xlsread('TX2007.xls',7,'E1:E8760');
TXHourly=[O51;O52;O53;O54;O55;O61;O62;O63;O64;O65;O66;O67;O68;O69;O71;O72;O73;O74;O75;O76;O77;O78;O79;O710;O711;O712];

Wednesday, June 10, 2009

2nd version of the epa ozone data code

 %This program runs faster than the 1st version
%The data output is in a better form, no cell arrays are involved.
%When the data record is not a number but a string, it converts that record into NaN. In the 1st version, it onverts that into 0.

%This program loads a series of file of ozone data
clear all; close all; clc;
[TXSt TXCo TXSi]=textread('TXSites.txt','%n %n %n','delimiter','|');
[NJSt NJCo NJSi]=textread('NJSites.txt','%n %n %n','delimiter','|');
[LASt LACo LASi]=textread('LASites.txt','%n %n %n','delimiter','|');
FN=textread('filenumber.txt','%s');
TXData=[];NJData=[];LAData=[];%define arrays
for i=1: 161
    i
    [St Co Si Da Le]=textread(FN{i},'%*s %*s %n %n %n %*s %*s %*s %*s %*s %n %*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s','delimiter','|');
    [r c]=size(St);
for j=1:r
   if (ismember(St(j), TXSt))&&(ismember(Co(j), TXCo))&&(ismember(Si(j),TXSi))
       TXData=[TXData; [St(j), Co(j), Si(j), Da(j), str2double(Le(j))*1000]];
   else if (ismember(St(j), NJSt))&&(ismember(Co(j), NJCo))&&(ismember(Si(j),NJSi))
           NJData=[NJData; [St(j), Co(j), Si(j), Da(j), str2double(Le(j))*1000]];
       else if (ismember(St(j), LASt))&&(ismember(Co(j), LACo))&&(ismember(Si(j),LASi))
               LAData=[LAData; [St(j), Co(j), Si(j), Da(j), str2double(Le(j))*1000]];
           end
       end
   end
end
end

The first code used to fetch the epa ozone data

%This program loads a series of file of ozone data
clear all; close all; clc;
TXSites=textread('TXSites.txt','%s');
NJSites=textread('NJSites.txt','%s');
LASites=textread('LASites.txt','%s');
year='2006';
NofF='161'; %number of total files
US='_'; %underscore
LQ='('; %lefrquote
RQ=')'; %rightquote
SF='.txt'; %surfix
n=textread('number.txt','%s'); %read 001, 002,003 ...
FN=strcat(year,US,n,LQ,NofF,RQ,SF);%generate FileName
State=[];County=[];Site=[];Date=[];Time=[];Level=[]; %define arrays
TXData=[];NJData=[];LAData=[];%define arrays
for i=1: 161;
FID=fopen(char(FN(i)));
A=textscan(FID,'%*s %*s %s %s %s %*s %*s %*s %*s %*s %n %s %n %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s... %*s %*s %*s %*s','delimiter','|');
State=A{1,1};
County=A{1,2};
Site=A{1,3};
Date=A{1,4};
Time=A{1,5};
Level=A{1,6};
[r c]=size(State);
 for j=1:r
    ID=strcat(State(j),County(j),Site(j));
   if ismember(ID, TXSites)
       TXData=[TXData; [State(j), County(j), Site(j), Date(j), Time(j), Level(j)]];
   else if ismember(ID, NJSites)
           NJData=[NJData; [State(j), County(j), Site(j), Date(j), Time(j), Level(j)]];
       else if ismember(ID, LASites)
               LAData=[LAData; [State(j), County(j), Site(j), Date(j), Time(j), Level(j)]];
           end
       end
   end
 end
end
beep;

Make your code run faster

1) Don't use loop whenever possible. Very likely there will be a function that can do the job that you want to do by using loops.

2) Stay away from cell array whenever possible. I don't understand the details, but handling cell array will take much more time than handling numbers.

3)Use faster CPU. But to my experience (though I am not quite experienced), Matlab will not use all the CPUs to do the work, even if you use a multi core cmputer.

Use Matlab to edit sounds, images and videos

imread and imwrite

wavread and wavwrite

video? I don't know. I am a newbie!

a small trick

When you don't exactly remember the command, you can use Tab key to help you find it out.

For example, you can try this: in command window, input mag and then press the Tab key. See?

This can also help you find some related functions and commands.

Good Resources

The best resource i found is the help command. The second best resource is Google! Yeah!

Dealing with huge data file

Recently I downloaded a huge text file, which is larger than 1GB...

The Notepad and Excel can't fully open that text file for me. Even Matlab can't do that for me. There's always memory overflow. Then I googled a lot and found that Matlab can only handle a certain size of memories. It won't help if you increase the RAM or the page file size on your computer.

After struggling a few days on that, my solution end up like this:

Use a file splitter (such as TextMaster) to split the text file into small pieces first. I set each piece contains 50,000 rows because Excel can open about 65,000 rows.

Then read the first piece, get the info I need and put them aside, then go and read the second pieces. For loop was used to let me open each and every file.

Find the best way to do your job

Although Matlab is believed (at least by myself) can do everything, usually the most efficient method to get your job done is combing something else with it. Human intelligence will always play the most important role.

Here's my own experience. I need to read data from a series of file with the name as:
  ABC_1(100).txt
  ABC_2(100).txt
  .
  .
  ABC_100(100).txt

It took me a lot of time to write a piece of code that can generate the file names in Matlab and then read the files one by one. Actually what I could have done is just manually input these file names into a text file and then read the file names from it. That may look like a dumb method to you, but in fact, it may take much less time for me.  

Matlab Basics : dealing with arrays

Matlab is so powerful at handling arrays. It can generate lots of different kind of arrays, take one or some of specific elements in array and change the value of the elements. Since the data and files saved in computer are all basically 0 and 1, I guess that every file in the computer can be interpreted as certain kind of array, and thus can be accessed and edited by Matlab. (Of course, it's my GUESS.)

How to generate array using Matlab?

mauanlly input: A1=[1 NaN 3; 4 NaN 6; 7 NaN 9] % NaN means Not a Number.
use functions:

A2=magic(3) % magic array
A3=zeros(3) % all elements are0
A3=ones(3) % all elements are 1

my-alpine and docker-compose.yml

 ``` version: '1' services:     man:       build: .       image: my-alpine:latest   ```  Dockerfile: ``` FROM alpine:latest ENV PYTH...