【问题标题】:How to use row by row value for calculation from .mat file如何使用 .mat 文件中的逐行值进行计算
【发布时间】:2015-04-23 15:56:13
【问题描述】:

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;

【问题讨论】:

  • 请将您的代码减少到最小可重现示例
  • 请在上面找到代码'cycode.m'的小版本,

标签: matlab


【解决方案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 基础教程都解释了该语言中的矢量化操作

【讨论】:

  • 当我加载 .mat 文件然后从该文件中提取第一行值(有六个列因此六个值)然后在我的公式中使用这些值将产生一个答案。然后对 .mat 文件的第二行值执行它,然后它会给出第二个答案,依此类推........最后我想调用我需要的答案。
  • 请在上面找到代码“cycode.m”的小版本。
  • 能否请您对您的第一个建议再明确一点?
  • 在第一个建议中,变量data 以原始顺序保存“当前”行的所有值,仅此而已(inputMatrix 没有其他部分)。因此,在那个地方,很容易将该变量传递给例如一个函数,该函数只计算您对该单行数据所需的任何内容,而无需处理其余的输入。
  • outputValue 实际上是为inputMatrix 的每一行保存结果的向量。在我的代码中,它是动态的,也就是说,它会随着您向其添加结果值而扩展(但不是最好的方法,但对于大尺寸来说会变慢)。您可以使用outputValue = zeros( size( inputMatrix,1), 1); 启动它,而不是使用[]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多