简体   繁体   中英

issue working with 3d arrays in matlab

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 ??

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 ? cuz defining them like this in the script caused the

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. 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.

Secondly, if exploreff is preallocated as a numeric array you can't just start addressing it as a structure. 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...).

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) . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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