如何从.mat文件中逐行使用值进行计算

时间:2015-04-22 06:23:13

标签: matlab

function [inilife,ini_cycle] = cycode(a,b,c)

ld =load('allstresscomp.mat');% loading .mat file of(81290 rows and 6 colomns)
% from this .mat file I want extract row by row values(6 values of column)

ini_cycle=zeros(length(ld),1);

for Z=1:1:length(ld(:,1))
        sigc11=ld(Z,1);
        sigc12=ld(Z,6);
        sigc13=ld(Z,5);
       	sigc22=ld(Z,2);
        sigc23=ld(Z,4);
        sigc33=ld(Z,3);
            
    sigc = [sigc11; sigc12; sigc13; sigc22; sigc23; sigc33];   
        
    matstress = a*sigc;
    
    sigm1 = matstress(1,1);
    sigm2 = matstress(2,1);
    sigm3 = matstress(3,1);
    sigm23 = matstress(4,1);
    sigm31 = matstress(5,1);
    sigm12 = matstress(6,1);
   
    It = [sigm2 + sigm3 + sqrt((sigm2 +sigm13)^(2)- 4*(sigm12*sigm23 + sigm1^(2)))]/2;
    
    N(Z) = It + c - b; % this is one answer for values of one row of .mat file
    ini_cycle(Z,1)=N(Z); 
end 
inilife = N;

我是Matlab尝试一个小代码的初学者。我的.mat文件(allstresscomp.mat)大小为[81920(rows),6(columns)]。如何在代码和代码中使用.mat文件第一行的所有6个值?然后找到答案。再次使用代码中第二行.mat文件的所有6个值然后找到答案,依此类推,直到最后81290行&在某些variable 'N'中逐行保存这些答案。最后,我可以从'N'为我要求的任何行获取答案吗?我附上了'cycles_code.m'

function [inilife,ini_cycle] = cycles_code(cc, cf, cm,sif,sim,bt,bs1,bs2,freq,actvol,planck,U,k,T,loadratio)
    d = cc-cf;
    e = cc-cm;
    f = inv(d);
    A = -(sim/sif)*(f\e);
    I = eye(6);
    g = (sim*I)+(sif*A);
    h = inv(g);
    ld =load('allstresscomp.mat');
    ini_cycle=zeros(length(ld),1);
     for Z=1:1:length(ld(:,1))
            sigc11=ld(Z,1);
            sigc12=ld(Z,6);
            sigc13=ld(Z,5);
            sigc22=ld(Z,2);
            sigc23=ld(Z,4);
            sigc33=ld(Z,3);
            N = 0;
        sigc = [sigc11; sigc12; sigc13; sigc22; sigc23; sigc33];   
        j = h\inv(cc)*sigc;      
        matstress = cm*j;
        sigm1 = matstress(1,1);
        sigm2 = matstress(2,1);
        sigm3 = matstress(3,1);
        sigm23 = matstress(4,1);
        sigm31 = matstress(5,1);
        sigm12 = matstress(6,1);
        It = [sigm2 + sigm3 + sqrt((sigm2 +sigm3)^(2)- 4*(sigm2*sigm3 + sigm23^(2)))]/2;
        Is1 = sigm12^(2) + sigm31^(2);
        Is2 = 0.25*(sigm2-sigm3)^(2) + sigm23^(2);
        At = bt/bs1;
        As = bs2/bs1;
        effstress = sqrt(At*It^(2) + Is1 + As*Is2);
        sigmax = effstress;
        sigmin = loadratio*sigmax;
        actvoll = actvol/(6.02214179*10^(23));
        V = U/(6.02214179*10^(23));
        a = (actvoll*sigmax)/(k*T);
        b = (actvoll*sigmin)/(k*T);
        p = a*log10(exp(1));
        q = log10(1-2^((b*log10(exp(1)))-(a*log10(exp(1)))));
        c = p+q;
        d = (V/(k*T))*log10(exp(1));
        e = log10((freq*actvoll*planck*(sigmax-sigmin))/(k*T)^2);
        N(Z) = e+d-c;
        ini_cycle(Z,1)=N(Z);
    end 
    inilife = N;

1 个答案:

答案 0 :(得分:1)

如果您读取了.mat值并且输入矩阵被称为inputMatrix,那么您可以1)循环遍历行

outputValue = []
for rowIndex = 1:size( inputMatrix, 1)
   data = inputMatrix( rowIndex, :)

   outputValue( rowIndex) = result of calculation on data
end

2)更好,矢量化的东西。 我不会为你的问题写一个解决方案,因为这在很大程度上取决于你的计算。例如,如果您的输出是一行中的值的总和,那么就是单行

outputValue = sum( inputMatrix, 2) % along the second dimension - horizontal

outputValue = inputMatrix( :, 1) .* inputMatrix( :, 2) + inputMatrix( :, 3) .^ 2

。在这个例子中,符号不是必需的(逐个元素),但增加了可读性。

Matlab的任何基础教程都用语言解释了矢量化操作