replace elements in columns of a matrix

조회 수: 2 (최근 30일)
Tiina
Tiina 2015년 12월 2일
편집: Tiina 2015년 12월 8일
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

채택된 답변

Thorsten
Thorsten 2015년 12월 2일
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
Tiina
Tiina 2015년 12월 3일
편집: Tiina 2015년 12월 3일
thats right, thanks. I guess the only way is to adjust Thorstens code to run by block!? store calculations per block then concatenate. Could you advise on efficient ways?
Image Analyst
Image Analyst 2015년 12월 3일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 12월 2일
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by