2

I can define my data structure by two different ways:

Number 1:

for i = 1:3, matrix.a{1,i} = rand(1,2000000); end
for i = 1:3, matrix.a{2,i} = rand(1,2000000); end
for i = 1:3, matrix.g{2,i} = rand(1); end
for i = 1:3, matrix.g{1,i} = rand(1); end

Number 2:

for i = 1:3, matrix2(1,i).a = rand(1,2000000); end
for i = 1:3, matrix2(2,i).a = rand(1,2000000); end
for i = 1:3, matrix2(1,i).g = rand(1); end
for i = 1:3, matrix2(2,i).g = rand(1); end

Is one of them a much more efficient way?

Thanks!

2 Answers 2

1

Check whos matrix matrix2, that will show you the memory usage of both alternatives.

Without spending too much thought it should be pretty similar - it certainly is compared to the memory you need for the random numbers.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no large difference, both contain 6 matrices with 2 000 000 elements, this is the important part and unchanged. You can compare execution time of the code (tic toc) and memory (whos) with this single code line x=rand(6,2000001) which obviously generates and stores the required amount of random numbers in the most efficient way. There is no significant difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.