Matlabの実践的な小さな関数のまとめ


このブログ投稿は、Matlab を使用する際に著者が独自のニーズに従って作成したいくつかの小さな関数を記録します。

Part.I 細胞相関

Chapter.I 空の文字セルを作成する

Matlab は空のセル行列を作成できますcell(a,b)が、作成後、それに格納される型は空の double です。空のセル行列を作成し、それに格納されるデータ型が char であるようにしたいので、次のような関数を書きました。

% get a null cell which is a*b dims
function data=kcell(a,b)
data=cell(a,b);
for i=1:a
   for j=1:b
       data(i,j)=cellstr(num2str(data{
    
    i,j}));
   end
end
end

第Ⅱ章 セル行列から文字行列へ

この関数は、charストレージ型のセル行列をchar行列に変換できます。これを直接使用すると、目的の行列ではなく、cell2matすべてのセルの内容が 1 つに結合されるためです注: 各セルの長さは同じである必要があります。charcharchar

% cell mat 2 str mat
function data=cellm2strm(cemat)
n=length(cemat);
data=[];
for i=1:n
    data=[data;cemat{
    
    i}];
end
end

第III章 セルマトリックスからダブルマトリックスへ

doubleデータの型がcell に格納されている場合は、cell2mat関数を直接使用してセル行列を二重行列に変換できます。charデータの型が cell に格納されている場合は、次の関数を使用できます。

% cell_char mat 2 double mat
function num=cell2num(data)
[a,b]=size(data);
num=zeros(a,b);
for i=1:a
    for j=1:b
        num(i,j)=str2double(data{
    
    i,j});
    end
end
end

Part.II 行列相関

第I章 マトリックス挿入要素

Matlab は行ベクトル/列ベクトルの特定の位置に値を挿入します

% insert num at ind in mat
function data=insert(mat,ind,num)
n=length(mat);
data(ind)=num;
data(1:ind-1)=mat(1:ind-1);
data(ind+1:n+1)=mat(ind:n);
end

第II章 num行列からchar行列へ

変換フォーマットは調整によって制御されます'%02d'この関数は特に衛星 prn 変換に適用されますが、他の側面に適用したい場合は、少し変更することができます。

% this function can trans numList to charSatList
function satlist=num2sat_char(sys,num)
satlist=[];
for i=num
    satlist=[satlist;sys,num2str(i,'%02d')];
end
end

第III章 数値行列からセル行列へ

上記の関数と同様です。データのタイプは、返されたセル行列に格納されますchar

%this function can trans numList to cellSatList
function satlist=num2sat_cell(sys,num)
satlist={
    
    };
for i=num
    satlist=[satlist,cellstr([sys,num2str(i,'%02d')])];
end
end

Part.III 文字列相関

Chapter.I フォルダ配下の全ファイルのファイル名の一部を取得する

最近、ディレクトリからすべてのファイルのファイル名の最初の数文字を抽出して に変換したいと考えていますchar私はずっと「原因と同じだ」""誤解していたことが分かりました。''今日はそれが違うことに気づきました。そして、ユニークな要素、ディレクトリ内のすべてのファイルの名前の取得など、新しいことを学びました...

% get sitename from a dir
function site=getSite_dir(enudir)
dirOutput = dir(fullfile(enudir));% 此行+下面一行=获取目录所有文件名
plyName = {
    
    dirOutput.name};       % get a cell mat
plyName = plyName(3:end);         % rm . and ..
n=length(plyName);
sitelist="";
for i=1:n
   fname=plyName{
    
    i};
   sitelist=strcat(sitelist," ",fname(1:4));
end
sitelist=unique(regexp(strtrim(sitelist), '\s+', 'split'));
%string2char
nsite=length(sitelist);
site=[];
for i=1:nsite
    tmp=char(sitelist(i));
    tmp=lower(tmp);
    site=[site;tmp];
end
end

注:regexp区切り文字はスペースであるため、 はstrになりますstr arraychar一重引用符で囲まれた文字列と二重引用符で囲まれた文字列ですstring。両者の間の変換は、名前によって行うことができます。char組み合わせに使用され[]string組み合わせに使用されますstrcmp

第 II 章では 2 つの文字のサイズを比較します。

C++文字列比較と同様の関数を実装できます。

  • str1<str2、1を返す
  • str1==str2、0を返す
  • str1<str2、-1 を返す
% util compare two strings 1>1 1=0 1<-1
function p=mstrcmp(str1,str2)
k=min(length(str1),length(str2));
for n=1:k   %Compare the top k
    if(str1(n)>str2(n))
        p=1;break;
    elseif(str1(n)==str2(n))
        p=0;
    else p=-1;break;
    end
end
if(p==0)
    if(length(str1)>length(str2)) %The first k bits are equal, but str1 is longer
        p=1;
    elseif(length(str1)==length(str2))
        p=0;
    else p=-1;
    end
end
end

第三章 検索インデックス

行列prn内のインデックスを見つけるchar

sat はchar型、satlist はchar行列で、インデックス値を返します。

%this function can find the sat index from a charSatList
function prn=find_sat_char(satlist,sat)
n=size(satlist,1);
prn=0;
for i=1:n
    if mstrcmp(satlist(i,:),sat)==0
        prn=i;
        break;
    end
end
end

セル行列内の prn のインデックスを検索します。

sat はchar型、satlist はcell行列で、インデックス値を返します。

%this function can find the sat index from a cellSatList
function prn=find_sat_cell(satlist,sat)
n=length(satlist);
prn=0;
for i=1:n
    if mstrcmp(satlist{
    
    i},sat)==0
        prn=i;
        break;
    end
end
end

Part.IV 構造関連

第I章 構造を読む

たとえば、このようなファイルを構造に読み込みたいとします。ファイルの内容は次のとおりです。

name     sex       age     hobby
aa       man       4       8
ab       wom       5       9
bb       wom       6       10
cc       man       7       11

実装関数は次のとおりです。

% get struct from clkdif file
function stu=read_dif(file)
fid=fopen(file,'r');
str = fgetl(fid);
S = regexp(str, '\s+', 'split');  %field
n=length(S);
j=1;
while ~feof(fid)
    str = fgetl(fid);
    str=strtrim(str);    %rm the blankSpace on the beg and end
    if str(1)=='-'
        continue;
    end
    if str(1:3)=='EOF'
        break;
    end
    tmp = regexp(str, '\s+', 'split');
    for i=1:n
        eval(['stu(j).',S{
    
    i},'=tmp{i};']);
    end
    j=j+1;
end
fclose(fid);
end

呼び出し例:

clc;clear;
file='C:\Users\OHanlon\Desktop\a.txt';
tic;
stu=read_dif(file);
toc;

ここに画像の説明を挿入

第II章 構造体のフィールドの値をすべて取得する

上記機能と併用可能

% get the field value from sta
function data=get_fdata(sta,field)
data=[];
af=fieldnames(sta);
n=length(af);
for i=1:n
    if mstrcmp(af{
    
    i},field)==0
        break;
    end
end
if (mstrcmp(af{
    
    i},field)~=0)
    disp('----error----');
    disp(['The filed ''',field,''' is not in sta!']);
    disp('------end----');
    return;
end
m=length(sta);
for i=1:m
    data=eval(['[data;sta(i).',field,'];']);
end
end

Part.V データ処理

第I章 加重平均の計算

この関数は、一連のデータの加重平均を計算できます。入力データには 2 つの列があります。最初の列は加重平均を計算するデータのセットで、2 番目の列はそれに対応する重みです。重みは、 function なので、その絶対サイズを気にする必要はありません。関数は加重平均を返します。

% Util: compute ave with weight
function ave=ave_weight(data)
std=data(:,1);
weight=data(:,2);
weight=weight./sum(weight);
ave=sum(std.*weight);
end

おすすめ

転載: blog.csdn.net/Gou_Hailong/article/details/129239442