Reputation: 311
I'm using the code below to replace random-looking columns in a matrix with zeros (in reality they're not random, but correspond to degrees of freedom of human movement). The code works, but I feel like there has to be a more efficient way to do this. The matrix has 128 columns and I'm not done yet!
subject(subNum).trial(trialNum).Qsagittal = subject(subNum).trial(trialNum).Q;
for column = 1:9
switch column
case 2
subject(subNum).trial(trialNum).Qsagittal(:,column) = zeros(m,1);
case 3
subject(subNum).trial(trialNum).Qsagittal(:,column) = zeros(m,1);
case 5
subject(subNum).trial(trialNum).Qsagittal(:,column) = zeros(m,1);
case 6
subject(subNum).trial(trialNum).Qsagittal(:,column) = zeros(m,1);
case 8
subject(subNum).trial(trialNum).Qsagittal(:,column) = zeros(m,1);
case 9
subject(subNum).trial(trialNum).Qsagittal(:,column) = zeros(m,1);
end
end
Upvotes: 0
Views: 274
Reputation: 21563
As @jucestain suggested you don't need to update the columns 1 by one. Matlab allows you to enter a vector as indexing argument, so suppose you have:
idx = [2,3,5,6,8,9];
You can directly assign zero to all columns:
subject(subNum).trial(trialNum).Qsagittal(:,idx) = 0;
Just a guess, but it seems like you want each second and third number to be in idx
. You can achieve this easily like so:
idx = 1:9;
idx(1:3:9) = [];
For future reference, if someone actually wants to assign 6 out of 9 random columns to zero, it can be achieved by using this index:
idx = randperm(9,6)
Upvotes: 1