Is there an equivalent to R's negative indexing in Matlab? -


in r, if have vector , list of indices, can express idea want "all elements except these indices" using negative index. in particular, consider following r code:

data = rnorm(100) indices = sample(1:length(data), length(data)/2) training_data = data[indices] test_data = data[-indices] 

after code, sampled_data contains elements in data indices not included in indices. is there equivalent in matlab?

i tried directly using same syntax (of course wtih () instead of [], gave error

subscript indices must either real positive integers or logicals. 

matlab not allow negative indices. can remove elements this:

data2 = data; data2(indices) = [];  % remove selected elements 

but when doing machine-learning stuff prefer use logical indexing:

istest = randn(length(data), 1) < 0;   % random logicals: 50% 0's , 50% 1's istrain = ~istest; % operate on data(istest) , data(istrain). 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -