Home Matlab: stack all values under each other in double for loop
Reply: 1

Matlab: stack all values under each other in double for loop

Yoni Verhaegen
1#
Yoni Verhaegen Published in 2018-02-13 11:43:52Z

I'm running a double for loop for the calculation of solar radiation, something like:

for b=1:365      %amount of days
   for n=1:24    %amount of hours
   solar(b,n)=sin(ht(b,n))+...
   end
end

However, instead of the creation of a 365x24 martrix, I'd like a 8760x1 array where all the values are plotted under each other in 1 colomn. It is important that this happens within the for loop, as some other calculation need to be done on the array in this loop.

Thanks!

Will
2#
Will Reply to 2018-02-13 12:26:55Z

MATLAB matrices are inherently accessible as if they are 1-dimensional vectors via linear indexing. The linear order of matrix elements goes in ascending order of dimension, so to make your matrix order by hour and then by day, simply swap the dimensions you index into solar:

solar(n,b)=sin(ht(b,n))+...

If you explicitly need a 8760x1 array, you can then obtain this as solar(:). However if you simply needed to iterate over all elements in a single loop you can rely on linear indexing without reshaping the matrix:

for n = 1:numel(solar)
    % doSomething(solar(n));
end
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.434833 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO