-1

I have this work which I have to do by creating a sub-matrix out of a given data set. I will explain it below.

Suppose, I have the data set as:

100  200  300  400  500  600
101  201  301  401  501  601
102  202  302  402  502  602

So, I want to create sub-matrices as follows:

For the first iteration ->

[[101  201  301  401  501]
[102  202  302  402  502]]

and

[[601]
[602]]

For the second iteration ->

[[100  200  300  400  500]
[102  202  302  402  502]]

and

[[600]
[602]]

And so on... The process will continue till the number of rows in the main/starting matrix.

In short, I want a LOO (leave one out) implementation of this data set, so that I can further work on it.

If you guys have any idea on how to do it, please share it. :)

MaJoR
  • 954
  • 7
  • 20
  • You need to provide some code that you tried to use for solving problem. We're not here to solve others homework. – Andronicus Mar 02 '18 at 07:12
  • I wanted to actually use the concatenation operator, because it seems to do the trick. I was trying something like: `sub = [mat(1:1052) ; mat(5)]` But, it doesn't do the work. Any ideas? – MaJoR Mar 02 '18 at 07:22

3 Answers3

0

Assuming A is the main matrix, a1 and a2 will be your first set of sub-matrices and b1 and b2 will be the second set of sub-matrices.

>> A=[100  200  300  400  500  600
    101  201  301  401  501  601
    102  202  302  402  502  602];
>> a1=A(2:3,1:5)

    a1 =

   101   201   301   401   501
   102   202   302   402   502

>> a2=A(2:3,6)

a2 =

   601
   602

>> b1=A(1:2,1:5)

b1 =

   100   200   300   400   500
   101   201   301   401   501

>> b2=A(1:2,6)

b2 =

   600
   601
Hazem
  • 380
  • 1
  • 5
  • 14
  • Yup. That is what I was looking for. Usage of the concatenation operator. I've combined it with an iterative loop for bigger data sets. Thanks a lot! – MaJoR Mar 02 '18 at 10:13
0

A proper indexing is your friend here. For the given matrix:

X = [
   100  200  300  400  500  600
   101  201  301  401  501  601
   102  202  302  402  502  602
];

The first subsets are:

S1A = X(2:3,1:end-1);
S1B = X(2:3,end);

and the second subsets are:

S2A = X(1:3,1:end-1);
S2B = X(1:3,end);

Since you want to perform this for all the two-rows combinations of the matrix, the rows indexing patten can be generated with the nchoosek function as follows:

X_seq = 1:numel(x);
idx = nchoosek(X_seq,2);

Then, with an iteration (just to keep things simple... althrough in Matlab is always recommended to vectorize as many computations as possile), you can extract all the matches:

idx_len = size(idx,1);
res = cell(idx_len,2);

for i = 1:idx_len
    idx_curr = idx(i,:);
    res(i,:) = {X(idx_curr,1:end-1) X(idx_curr,end)};
end
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • It is not *always* recommended to *vectorise*. – Sardar Usama Mar 02 '18 at 10:08
  • Thanks for the helo @Tommaso .. However I got a simpler iterative solution. I will post it below. :) – MaJoR Mar 02 '18 at 10:11
  • Why is it not always recommended to vectorise the variables? @SardarUsama – MaJoR Mar 02 '18 at 10:11
  • @MaJoR21 It depends on the task at hand. Vectorised computations are memory intensive and if there is a constraint on memory, loops are the way to go. Furthermore, loops have been significantly improved by Mathworks in the newer versions and shouldn't be unnecessarily frowned upon. – Sardar Usama Mar 02 '18 at 10:15
0

If you have the stats and ML toolbox then you can use their built-in cross validation functions. See cvpartition or crossvalind

Dan
  • 45,079
  • 17
  • 88
  • 157