MATLAB: What is the elegent way to replace every Nth column in a matrix with another column beginnerMATLAB It looks easy with a for loop, but I am a beginner and was told to try and avoid for loops… I though abot something that looks like A(;,n:n:end) but I get a "dimensions mismatch error" Thank you Best Answer B = randn(10,10); % replace every 3rd column with x x = randn(10,1); N = 3; indices = 1:N:size(B,2); x = repmat(x,1,length(indices)); B(:,indices) = x;Probably you are trying to replace a matrix which you get with A(:,1:n:end) with a vector. The sizes have to match. Related SolutionsMATLAB: Replacing every columns of nan with the mean of each columns A simple vectorised way of achieving what you want:median1 = repmat(nanmean(combinedatamat), size(combinedatamat, 1), 1);combinedatamat(isnan(combinedatamat)) = median1(isnan(combinedatamat)) MATLAB: How to delete every nth row If your matrix is A, thenA(n:n:end,:) = [];will delete every nth row. Your matrix row count does not need to be evenly divisible by n. Related QuestionI have an all zero matrix ‘a’ of size m x n. In a new 2 column matrix ‘b’ I have the indices where I should replace the zero in ‘a’ with 1. Is there a way to do this without a ‘for’ loop? The m,n > 500..Adding a column every nth column without replacing existing oneMultiply col vector with matrixMy matrix dimensions do not agree in the loop. Not sure whyPicking the (n) th occurrence of a number in a vector or the (n)th row in a matrix.Flip certain columns in matrix
Best Answer