replace elements in columns of a matrix
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Thorsten
on 2 Dec 2015
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
3 Comments
Image Analyst
on 3 Dec 2015
If you're going to concatenate block by block (column by column) to get your 2-D array, then you won't have enough memory, like I explained in my answer. You're going to have to figure out a way to do what you need to do without having the 2D array all in memory at the same time. Why do you think the whole thing needs to be there? Why can't you deal with it column by column like Thorsten showed, and I also recommend? Just have a for loop over columns.
More Answers (1)
Image Analyst
on 2 Dec 2015
I am pretty sure the answer is no. The problem is that the nth row of B goes into the nth column of out. Like in your example, the 5th row of B goes into the 5th column of "out". So with millions of rows, you will have millions of rows (of course) but you will have millions of columns. That is just far, far too big - you will run out of memory. So with, say, 3 million rows, you'll have a 9*10^12 element array at 8 bytes per element - that's 72,000 gigabytes GB of RAM. I have 32 GB of RAM (more than most people) and just after I fire up MATLAB it tells me the biggest array I can have is 6 GB. Six is a long way from 72 thousand.
Why do you think you want this "out" all in memory at one time? Can't you just do your process one row at a time without computing "out" in advance? I suggest you figure out how to utilize "out" in a more clever way so that you don't have to have it all in memory at one time.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!