MATLAB: Replace elements in columns of a matrix

replace columns elements

Hi, I have columns block (which is a unique function output) A and B as below. I want to replace one element from column B in A each time (as in out) so that I ll do calculations on each column in out. Is there any way to do that without running out of memory possibly avoiding loops(A and B has millions of rows).The attachment demonstrates replacement up to block 2 but this continues all the way

Best Answer

This is the best I can think of
A = rand(1e5, 1);
B = rand(1e5, 1);
res = nan(size(A));
for i = 1:numel(A)
temp = A(i);
A(i) = B(i);
res(i) = mean(A); % or whatever you want to compute
A(i) = temp;
end
Related Question