Question about Recursively Adding Vectors to a Single Vector

조회 수: 1 (최근 30일)
Jackie Lee
Jackie Lee 2011년 10월 3일
Hello all,
I am a newbie in Matlab and would appreciate your time on my problem.
I would like to create an index which start with:
i.e., index0 = [0 7 10]';
And then, I would like to add 24 to each of the element in index0:
i.e., index1 = [0 7 10]' + 24;
Finally, I want to sum all the indices into one vector:
i.e., index = [index0;index1;...indexN]
However, I got the problem of "Out of memroy" when executing the following code. I think the code is farily simple, and I cannot figure out what is the reason? I would appreicate if you can advise the best way to do this?
Here is the code hat has the memory issue:
count = 1;
index = [0 7 12]'; % change value at hr = 0, 5, 11
while count <= 365
index_new = index + 24;
index = [index;index_new]; % store all data in one vector
count = count + 1;
end
By the way, this code was run on 32-bit Matlab 2009b and it will display memory issues, but if run at 64-bit Matlab 2011a then I have to manually shut down my comptuer and restart. There is no way to quit the Matlab and the computer is dead then. I feel frustrated about this issue since I have to reinforce the shut-down and restart every time.
Thanks a lot!
Jackie
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2011년 10월 3일
I don't think your code is right for your intent. try while count <= 3 first, check the value of index and modify your code to make it right first. There is probably a better way to do it but try your approach first.

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

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 10월 3일
For your reference:
a=zeros(3,366);
a(1,:)=0:24:(365*24);
a(2,:)=7+a(1,:);
a(3,:)=12+a(1,:);
index=a(:)
  댓글 수: 9
Fangjun Jiang
Fangjun Jiang 2011년 10월 3일
Then you'd better use the bsxfun().
c=bsxfun(@plus,(0:7)',[0 24 48])
Jackie Lee
Jackie Lee 2011년 10월 3일
That is great! Thanks for your help!!

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 10월 3일
You are doubling the amount of memory you use for index() in each iteration of the loop. Your computer cannot hold 3 * 2^365 elements -- that would be over 22 x 10^100 memory locations.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by