0

I want to create a target vector. In which I am having some problems. What I want is all the combinations of 3's of my 18 objects from T into Target. But It is unable to produce that combination. It is working for each combination individually but for me the "for loop" doesn't seem to work.

% T is a structure of 18 different sized objects  

% idx is index of size 816*3 double 
idx = combnk(1:18,3);    

% TNames is cell of size 18*1 
TNames = fieldnames(T);  

for i = 1:length(idx) 

  Target(:,:,i) = [T.(TNames{idx(i,1)}) ; 
                   T.(TNames{idx(i,2)}) ; 
                   T.(TNames{idx(i,3)}) ]; 

end

The above code is working fine when I delete (:,:,i) from Target(:,:,i) and write 1,2,3 ... so on ... in places of T.(TNames{idx(i,1)})..T.(TNames{idx(i,2)}) and T.(TNames{idx(i,3)})

I had tried different alternatives but I'm unable to fix this problem.. I was even able to create a target <3*859 cell> but it is not acceptable to neural net. It needs to be <1*859 cell> for the neural net to work properly.. so, can someone help me to fix this issue?

I have also tried this:

T is a <1*1 struct> inside which i have 18 fints(Financial Timeseries) objects of 859 rows each and different no of columns(ranging from 1 to 4 columns in each fints object)..I have converted all those fints objects to matrices through fts2mat command.

Now I have new matrix <32*859 double> with identifiable fints objects in each column ranges. i.e. I can identify that first four entries in each column belong to first fints object and next 3 entries belong to 2nd fints object and so on.

I want to grab all the combinations (816 combinations) of 3 fints objects (out of 18) in a new matrix in such a way that can finally convert it into <859*1 cell> or <1*859 cell> (more precisely) to provide that to Neural network for proper training...

This task has to be done through indexing because each combination of 3's should contains 3 fints objects entries with each fints objects having different no. of columns. And I have to garb all the columns of each fints object everytime in my 816 combinations and concatenate those 3 fints objects. taking all the columns of each fints object and putting it in rows.

In short I should have <1*859 cell> in which there are 816 different combinations of 3 fints objects (out of my T struct) and each combination should have 3 fints objects in it and each combination should be of <1*859 cell> to provide it to neural network for proper training. Since NN doesn't take different dimension cells..

I thank you in advance

  • 1
    Can you give an example of "differently-sized objects?" – sfstewman Jul 26 '12 at 23:28
  • 1
    Can you give an example of what `TNames` contains? It looks like what you're doing is placing a 3x1item (right hand side of the equals) into a space that is not 3x1. – Ben A. Jul 26 '12 at 23:28
  • T is a <1*1 struct> inside which i have 18 fints objects of 859 rows each and different no of columns.. I want to grab all the combinations(816 combinations) of 3 fints objects in a new matrix in such way that I get <859*1 cell> to provide that to Neural network... – shahab shah Jul 27 '12 at 10:24

1 Answers1

0

An example to illustrate the problem:

%# structure with 4 "differently sized objects"
T = struct('a',1, 'b',rand(2,2), 'c','string', 'd',{{1 2}})

%# what you are trying to do in the loop is:
[T.a T.b T.c]             %# <---- ERROR!

Which will complain with:

Error using horzcat
CAT arguments dimensions are not consistent.

MATLAB matrices/vectors can only store consistent elements of the same type. Here one is a scalar, the other a 2-by-2 matrix, the last one a string...

You want to use cell arrays:

TNames = fieldnames(T);
idx = nchoosek(1:numel(TNames),3);

C = cell(size(idx,1),1);
for i=1:size(idx,1)
    C{i} = {T.(TNames{idx(i,1)}) T.(TNames{idx(i,2)}) T.(TNames{idx(i,3)})};
end

For example the last combination would be:

>> C{end}
ans = 
    [2x2 double]    'string'    {1x2 cell}

corresponding to {T.b T.c T.d}

Amro
  • 123,847
  • 25
  • 243
  • 454
  • Thanks alot but I have to provide the C matrix as target to neural network in <1*859 cell> dimension.. while C here is <816*1 cell> so neural net is not taking it as my target output... – shahab shah Jul 27 '12 at 13:10
  • @shahabshah: my point was you should be aware of the size/type of "objects" you are trying to concatenate into one matrix. If they are not compatible/consistent, you have to store in cell arrays. I hope that point was made clear by the example above... – Amro Jul 27 '12 at 15:19
  • Thanks Amro: Your suggestion really worked for me by making little amendment to my code... Thanks Again... You are really helpful... – shahab shah Jul 28 '12 at 12:23