Question about Recursively Adding Vectors to a Single Vector

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 Kommentar

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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 3 Okt. 2011
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 Kommentare

index = bsxfun(@plus,[0 7 12]',(0:24:365));
index = index(:)
Who doesn't love bsxfun? :)
Yes. I thought about that but I am usually scared of @.
HiFangjun and Matt,
Thanks a lot for your help.
I will need to study more about the vectorization to avoid such memory issues in the future.
bsxfun is really a good function that I was not aware of.
Fangjun: why are you scared of using @, any disadvantages?
Sorry, Jackie. Nothing wrong with the @, function handle. I was trying to express a little bit of humor. Sometimes when people combine bsxfun(), cellrun() or arrayfun() with an anonymous function, it's a little hard to read and understand, thus I was scared. For example, do you read
a=cellfun(@(x) x.^2,b)?
all right! Thanks for your clarification and example.
I have a follow-up question, and it may looks simple for you, please bare with me on that:)
In your code, if I want to combine all the indices between the rows in the vector a, how should I do this. e.g.,
In your code (only add one row vector),
a(1,1:3) = 0:24:48
a(2,1:3) = 7:31:55
a(3,1:3) = 12:36:60
a(4,1:3) = 24:48:72
I am wondering how to extract and cobmine these indices in your "a" matirix, e.g., if I want the following vector b:
b = [0:7,24:31,48:55,...]
what would be the best method to do that? do I need a for-loop? if I use b = (1,1:3):a(2,1:3), the ans is only 0:7? I guess my answer is how to include all of them in one vector.
Thanks a lot!
Jackie
b=a(1:2,:), the way to reference a matrix is always a(row,col), so here, you want to select the 1st and 2nd row, and all columns, the column index can be 1:end, but : would do that.
Hi, I tried but I would like to also have the values in between, I just tried this: b=a(1:2,1:3), but it only gives me
b =
0 24 48
7 31 55
But what if I need the indices between the 0 and 7, 34 and 31, and so on like this:
b = [0 1 2 3... 7 24 25 26...31 48 49...55];
Thanks a lot!
Then you'd better use the bsxfun().
c=bsxfun(@plus,(0:7)',[0 24 48])
That is great! Thanks for your help!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 3 Okt. 2011

0 Stimmen

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.

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by