2

I am interested in seeing all the elements in the :

result(:,:).randMin(1:4,2:end)

in which the result(a=1:24, d=1:5).

In general is it possible to access them without a loop and cat ?

3 Answers 3

2

You cannot use the [] trick with multi-level indexing, but if all of randMin are 128 x 11 arrays:

out = [result(1:24,1:5).randMin];
out = reshape(out,[128 11, 24, 5]);
out = out(1:4,2:end,:,:);

Final result has size of 4 x 10 x 24 x 5 where the first two are your randMin(1:4,2:end), and last two dimensions are your a and d respectively.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you are looking for getfield:

getfield( result, {1:24, 1:5}, 'randMin', {1:4, 2:end} );

I'm a bit rusty with this command and you might need to play with it a bit to make it work. Read its manual and good luck!

1 Comment

tnx @Shai, but it only returns the first (a,d)
1

I don't think it is possible because randMin could be something else for every field in result. result(1,1).randMin could be a matrix, result(1,2).randMin could be a vector, result(2,1).randMin could be 4-dimensional...you see where I'm going with this.

So there is no way of knowing the dimensions or the size of each result's randMin without looping through all fields in result. If there is a function that does what you want, it will have to use a loop internally, so you might as well use a loop yourself.

Edit: If it is constant you can try something like this:

%Generating matrix struct results(a,b).randMin(c,d)
dim1=24;
dim2=5;
dim3=128;
dim4=11;

% value=0;
% for i=1:dim1
%     for j=1:dim2
%         for k=1:dim3
%             for l=1:dim4
%                 results(i,j).randMin(k,l)=value;
%                 value=value+1;
%             end
%         end
%     end
% end


%Getting the values
range1=1:24;
range2=1:5;
range3=1:4;
range4=2:dim4;

myMat=[results(range1, range2).randMin];  
myContainer=reshape(myMat, dim3, dim4, length(range1), length(range2));
desiredValues=myContainer(range3, range4,:,:);

In the end, desiredValues will have the values you want, but the indices switched sides, instead of results(a,b).randMin(c,d) it is now desiredValues(c,d,a,b).

As I didn't know exactly how your struct looks like, I defined dim1 to dim4 as maximum values for the indices a to d. You can use range1 to range4 to select your desired values.

2 Comments

Tnx for the reply, yes I totally understand, but the point is here accidentally all the randMin are the same result(a,d).randMin is a array of (128 11)
@Amir In this case you can use the code above. It generates a struct that is similar to yours and then you can select the ranges you want to get the desired values. I hope it helps. Maximum size is called dim1 to dim4 and the range you want is called range1 to range4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.