1

How do I create a concatenated matrix in Matlab which would give the result below?

[0.01 0.01 error1]
[0.01 0.03 error2]
...
[30 30 error64]

So far, what I have tried is below.

C =  [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];
sigma =  [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30];

m = zeros(64,3);

for ci = C
for si = sigma
    train = svmTrain(X, y, ci, @(x1, x2) gaussianKernel(x1, x2, si));
    pred = svmPredict(train, X);
    error = mean(double(pred ~= y))    
    m = [m ; [C,sigma,error]];    
end
end

I expect a 64 X 3 column matrix.

1 Answer 1

3

It seems like the code you provided basically does that. You should be starting off with an empty matrix m.

m = []; % Initialize empty result matrix m 

for ci = C
    for si = sigma
        error = % Calculate error here
        m = [m ; [C,sigma,error]];  % concatenate new row onto m.
    end
end
Sign up to request clarification or add additional context in comments.

4 Comments

I am just getting confused i guess.
I was expecting a 64 X 3 matrix
If the length(C) * length(sigma) is 64 you should get a 64 x 3 matrix at the end.
I'll make a small performance comment. This doesn't matter for a 64x3 matrix, but if you're matrix is HUGE (i.e. tens to hundreds of thousands of rows), the concatenation step m = [m; blah]; gets quite costly: you're constantly rewriting the whole m matrix. An alternative is something like m = zeros(n_rows, n_cols) then in each step of loop, m(k_start:k_end, :) = [C, sigma, error]. This would copy the right data into the right spot each loop (compared to rearranging the entire m matrix each loop).

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.