简体   繁体   English

在matlab中使用3d数组的问题

[英]issue working with 3d arrays in matlab

First Question : First Question

regarding my code here : 关于我的代码:

for z = [1 2 4 8 12 16 24 32 64 96 128]    
    for a=1:24
        a;
        for d=1:5
            d;
           [result]=evolRand(128,1,10,1,bench);
           bb= 5*(a-1)+d;
           temp=eval(['minExe_useModel_' num2str(z)]);
           %z=num2str(zz)
           exploreff(a,d,z) =mean(mean(result(a,d).randMin(:,2:end)))/temp(bb);
        end
    end
end

why at the end I am getting : 为什么最后我得到:

>>size(exploreff)

ans =

    24     5   128

while I had assigned z= [1 2 4 8 12 16 24 32 64 96 128] which was 11 ?? 虽然我已经指定z= [1 2 4 8 12 16 24 32 64 96 128]这是11 ??

Second Question : Second Question

How am I gonna be able to define a array of structure out of these z so that I can call them like exploreff(a,b).z ? 我怎么能从这些z中定义一个结构数组,以便我可以像exploreff(a,b).z一样调用它们? cuz defining them like this in the script caused the cuz在脚本中定义它们就像这样导致了

Structure assignment to non-structure object.

Error in explorationEffort_Speedup (line 15)
           exploreff(aa,dd).z=mean(mean(result(aa,dd).randMin(:,2:end)))/temp(bb);`

error. 错误。

Firstly, 首先,

exploreff(a,d,z)

uses a , d and z as indices , therefore you're assigning 11 values to the 1st, 2nd, 4th, ... 96th and 128th indices along the 3rd dimension. 使用adz作为索引 ,因此您要沿第3维向第1,第2,第4,......第96和第128个索引分配11个值。 Matlab automatically expands (and zero-fills) the array when you assign to an index outside its current dimensions, hence why the 3rd dimension ends up at 128 elements long. 当您分配到当前维度之外的索引时,Matlab会自动扩展(并填充零)数组,因此第三维最终会以128个元素长度结束。

Secondly, if exploreff is preallocated as a numeric array you can't just start addressing it as a structure. 其次,如果将exploreff预先分配为数字数组,则不能仅将其作为结构进行寻址。 If you preallocate it as a struct array (using struct ) first, then dynamically expanding it and adding fields in that way should be ok (I only have Octave to test, and that lets me do eg a(2,3).z = 5 straight off but I seem to recall Matlab wanting either the index or the field to exist first - that was 2007a though...). 如果你首先将它预先分配为结构数组(使用struct ),那么动态扩展它并以这种方式添加字段应该没问题(我只有Octave来测试,这让我做了例如a(2,3).z = 5直接但我似乎记得Matlab想要索引或字段首先存在 - 那 2007a虽然......)。

I think this is what you want to do... 我想这就是你想做的......

z = [1 2 4 8 12 16 24 32 64 96 128];

for i = 1:length(z)
    for a=1:24
        a;
        for d=1:5
            d;
           [result]=evolRand(128,1,10,1,bench);
           bb= 5*(a-1)+d;
           temp=eval(['minExe_useModel_' num2str(z(i))]);
           %z=num2str(zz)
           exploreff(a,d,z(i)) =mean(mean(result(a,d).randMin(:,2:end)))/temp(bb);
        end
    end
end

exploreff(a,d,z==8)

Note that z in the loop is replaced by z(i) . 请注意, z在循环被替换为z(i) Instead of directly specifying the value you want, you need to specify the index of the element. 您需要指定元素的索引,而不是直接指定所需的值。 In the last line, z==8 specify the index of the element in vector z that has 8 value. 在最后一行中, z==8指定向量z中具有8值的元素的索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM