create a vector without for
Show older comments
The vector structure is like [a b c d e f g h i j]
a=1:10
b=1:10
...
j=1:5
vect=[]
I can use for a =1:10
for b=1:10
...
vect = [vect;a.b...j]
end
end
but it looks not elegant. Is there a better way to generate it?
8 Comments
David Goodmanson
on 19 Jan 2020
Hi saihua,
I doesn't appear from what you put down that you are saving the results of each computation. But suppose you did save the results. With 10 variables a-j, even if each one only went from 1:5, that is already almost 10^7 items. If some of the limits are 1:10 then of course the result is even bigger. What is your goal here?
vincent lin
on 19 Jan 2020
David Goodmanson
on 19 Jan 2020
a and b go 1:10 and j goes 1:5. In order to talk specific sizes, could you say what the limits are for c through i?
vincent lin
on 19 Jan 2020
David Goodmanson
on 19 Jan 2020
Hi saihua,
There is no possible way you can build up vect by concatenating at the end. Each time you concatenate, Matlab has to reallocate memory. That would take forever. You would have to preallocate memory into an array, and you could add each set of ten new numbers by addressing the array.
But it doesn't matter anyway. The code will run through all the nested do loops and append to vect a total of 1.4175e8 times, which i'll abbreviate to 1.4e8 times. Each time you are adding 10 numbers, for a total of 1.4e9 real numbers. At 8 bytes per real, that is 11 gigaBytes of storage. If you use the uint8 option you can store the numbers at 1 byte per real, so you 'only' have 1.4 gigabytes of memory. I don't know how long it would take to process and store each set of 10 numbers, but if you could do it in 1msec, the whole calculation would take a couple of days. Again I would like to ask, what is the purpose of such a calculation?
Image Analyst
on 19 Jan 2020
So do you not want [a b c d e f g h i j] like you said, with a being a 10 element vector, b being another vector etc. (I gave my answer below assuming that.) David is assuming that a is really one element of your "a" (not the whole vector) and takes on every value in that a, so basically you have every possible permutation of all a values, etc.?
vincent lin
on 19 Jan 2020
Stephen23
on 19 Jan 2020
"I'd like to have possible permutation"
Then you should have asked about generating permutations: http://xyproblem.info/
Accepted Answer
More Answers (1)
Image Analyst
on 19 Jan 2020
Try
a = 1 : 10
b = 1 : 10
...
j = 1 : 5
outputVector = [a, b, c, d, e, f, g, h, i, j]
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!