I have a matrix (I guess in MatLab you call it a struct) or data structure:
data: [150x4 double]
labels: [150x1 double]
here is out my matrix.data looks like assume I do load my file with the name of matrix:
5.1000 3.5000 1.4000 0.2000
4.9000 3.0000 1.4000 0.2000
4.7000 3.2000 1.3000 0.2000
4.6000 3.1000 1.5000 0.2000
5.0000 3.6000 1.4000 0.2000
5.4000 3.9000 1.7000 0.4000
4.6000 3.4000 1.4000 0.3000
5.0000 3.4000 1.5000 0.2000
4.4000 2.9000 1.4000 0.2000
4.9000 3.1000 1.5000 0.1000
5.4000 3.7000 1.5000 0.2000
4.8000 3.4000 1.6000 0.2000
4.8000 3.0000 1.4000 0.1000
4.3000 3.0000 1.1000 0.1000
5.8000 4.0000 1.2000 0.2000
5.7000 4.4000 1.5000 0.4000
5.4000 3.9000 1.3000 0.4000
5.1000 3.5000 1.4000 0.3000
5.7000 3.8000 1.7000 0.3000
5.1000 3.8000 1.5000 0.3000
And here is my matrix.labels look like
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
I am trying to create 10 cross fold validation without using any of the existing functions in MatLab and due to my very limited MatLab knowledge I am having trouble going forward with from what I have. Any help would be great.
This is what I have so far, and I am sure this probably not the matlab way, but I am very new to matlab.
function[output] = fisher(dataFile, number_of_folds)
data = load(dataFile);
%create random permutation indx
idx = randperm(150);
output = data.data(idx(1:15),:);
end
Hahaha sorry, no solution. Don't have MATLAB on me right now so can't check code for errors. But here's the general idea:
index = 1; subsample = 1; newmat = zeros("150","6")
< 150 is the number of samples, 6 = 4 wide data + 1 wide labels + 1 we will use laterwhile ( length(labels) > 0 )
randNum = randi(length(labels))
? I think that's a random int that goes from 1 to the size of your labels array (it could be 0, please check the doc - if it is, do simple math to make it 1 < rand < length)newmat(index,:) = [data(randNum,:) labels(randNum) subsample]
< that last column is the subsample number from 1-10data(randNum,:) = []; same for labels
< note this will physically remove a row from the matrices, which is why we have to use a while loop and check for length > 0 rather than a for loop and simple indicesindex = index + 1; subsample = subsample + 1;
At the end of this, you should have a large data matrix that looks almost exactly like your original, but has randomly assigned "fold labels".
EDIT: code placed in more accessible manner. NOTE it's still pseudo-y code and is not complete! Also, you should note that this is NOT AT ALL the most efficient way, but shouldn't be too bad if you can't use matlab functions.
EDIT FOR ANSWER #2:
Ok another way, is to create a vector that is as long as your data set
Then, looping for that long (150), assign labels to random indices!
EDIT FOR ANSWER #2.5
EDIT FOR RANDPERM
generate the indices with randperm
now just assign
Here is my take for this cross validation. I create dummy data using magic(10) also I create labels randomly. Idea is following , we get our data and labels and combine them with random column. Consider following dummy code.
If we sort X according column 1, we sort our data randomly. This will give us cross validation randomness. Then next thing is to divide X according to cross validation percentage. Accomplishing this for one case easy enough. Lets consider %75 percent is train case and %25 percent is test case. Our size here is 4, then 3/4 = %75 and 1/4 is %25.
But accomplishing this a bit harder for N cross folds. Since we need to make this N times. For loop is necessary for this. For 5 cross folds. I get , in first f
Following code is an example for this process: