MATLAB: Selecting Every Other Element in a Matrix elementevery othermatrix How can I select every other element in a matrix, such as A=[1 2 8 7 6 5 4 6]? Best Answer I’m not certain what you want, so I’ll venture two possibilities:A=[1 2 8 7 6 5 4 6];Aodd = A(1:2:end); % Odd-Indexed ElementsAeven = A(2:2:end); % Even-Indexed Elements Related SolutionsMATLAB: Writing a loop “for” With a loop:phi = zeros(1, 452); % Instead of 450?s = 0;m = 1;for k = 1:4:452 phi(k:k+4) = (a_locs(k:k+4) - s) / a_odd(m); s = s + a_odd(m); m = m + 1;endAnd vectorized - assuming that a_odd is a row vector:phi = reshape(a_locs, 4, 113);phi = bsxfun(@minus, phi, [0, cumsum(a_odd(1:112)]);phi = bsxfun(@rdivide, phi, a_odd(1:113));phi = reshape(phi, 1, 452); MATLAB: Create a new array from an intersection of two previous arrays >> A = [1 1 1 1 4 4 4 4 6 6 6 6 6 6];>> B = [2 2 2 2 2 2 2 2 2 2 7 7 7 7 7 7 7 7 8 8 8 8 9 9 9 9];>> N = min(numel(A),numel(B));>> C = [reshape([A(1:N);B(1:N)],1,2*N),A(N+1:end),B(N+1:end)]C = 1 2 1 2 1 2 1 2 4 2 4 2 4 2 4 2 6 2 6 2 6 7 6 7 6 7 6 7 7 7 7 7 8 8 8 8 9 9 9 9 Related QuestionHow to separate odd and even elements of a matrix with out using for or while loops.How to separate a column into rows based on the element of a different column
Best Answer