I have Matrix A with n,n dimensions. How can I replace every second column of A with column vector x (of size n).
I want to do it without any "for/while" loops,
can someone please help me? thanks.
%// Create example data n = 21 A = magic(n) x = ones(size(A,1),1); %// Replace every second column of A with x starting from the first column m = ceil(size(A, 2)/2); X = x(:, ones(1,m)); %//Replicate x A(:,1:2:end) = X %// Put x in each odd column. If you want it to start from the second column then you must use floor instead of ceil
%//Create example data n = 6 A = magic(n) x = ones(n,1); %// Replace every second column of A with x starting from the second column m = floor(size(A, 2)/2); X = x(:, ones(1,m)); A(:,2:2:end) = X x to a matrix X that has columns equal to the number of columns you are assigning to (and the same rows obviously)