Currently I am working with a table containing the hourly meteorological data, specifically related with the sun position during a certain year. My table contains a cell array constituted by 6 structures: ‘Time (hour)’, ‘As’, ‘as’, ‘DHI’, ‘DNI’, ‘GHI’. Each structure contains a bit more of 8000 fields representing the data at each hour of an entire year. My objective is to calculate the tilt angle (theta) and orientation of a PV module (Azimuth of the module, Am), by optimizing them for a full year time period. To do so, I need to use a nested for loop, in order to determine at which theta and Am values the maximum energy values is received by the module. I’ve used the following code:
location_filename = 'Delft.mat';
load(location_filename,'As','as','DHI','DNI','GHI') % Loading vectors 'As', 'as', 'DHI', 'DNI' and 'GHI'
alpha = 0.2;
Am = 0:2:360;
theta = 0:2:90;
am = 90-theta;
for a = 1:length(am)
for b = 1:length(Am)
cos_AOI = cosd(am(a)).*cosd(as).*cosd(Am(b)-As)+(sind(am(a)).*sind(as));
Gdirect = DNI.*cos_AOI; % I obtained a column vector of 8700x1
Gdirect(Gdirect<0) = 0;
SVF = (1+cosd(theta))/2;
Gdiffuse = SVF.*DHI;
Galbedo = GHI.*alpha.*(1-SVF);
% Total Irradinace
Gm = Gdirect+Gdiffuse+Galbedo; % Here I obtained an array of 8700x46
%%%% {Total Energy: First, Gm is expressed in Watts(W), but I need to calculate the total energy received during the whole year at each theta and Am values. So, I think I should sum up all the rows of each column from the resulting Gm array. Thus, I would obtain the total energy in W-hour units for the whole year}%%
Em(a,b) = sum(Gm,1); % ERROR! “Subscripted assignment dimension mismatch”
end
end
% Optimum tilt and orientation for PV module
[Em_max,I] = max(Em(:))
% tilt angle of the PV module
[I_row,I_col] = ind2sub(size(Em),I)
% This will give me the row and the column index of Em array, but how do I know if they are the coordinates for Opt_theta or Opt_Am?
Opt_Am = %I don’t know how I could associate the index values for determine theta and Am.
Opt_theta =
I know that I have a mistake in my nested For Loop but I really can’t figure out what is wrong in my code. If some one can give me some comments in order to solve this problem I would be very grateful. Thanks in advance!!

 Respuesta aceptada

dpb
dpb el 10 de Ag. de 2018
Em(a,b) = sum(Gm,1); % ERROR! “Subscripted assignment dimension mismatch”
sum(Gm) (equivalent to sum(Gm,1) ) will return row vector of sums which you're trying to store in a single location. Do you want the overall sum or the one over the columns; I've not tried to decipher the code further?
Em(a,:) = sum(Gm); % save column sums
Once you've made the summation, you've lost the granularity of every individual position in the a,b loops.

7 comentarios

Luis Duarte
Luis Duarte el 11 de Ag. de 2018
Thanks for the comments. I tried running the code with the line you sugested me:
Em(a,:) = sum(Gm); But I obtained the same error message “Subscripted assignment dimension mismatch”. However, I think I solved part of the problem. When I delete the (a,b) indices from the line, I mean, I run Em = sum(Gm,1); it works!, but the following line not as I need. I mean, Gm output are several arrays each of 8700x46, each array resulting from the looping in the cos_AOI line. Then, for the Em = sum(Gm,1); the output is a row vector 1x46 for each loop, so, I got several Em (1x46) rows, each one resulting from each loop. Now, the problem is that in the following line of my code, [Em_max,I] = max(Em(:)) I want to calculate the maximum value of Em contained in any of the 1x46 rows produced during the looping. However, the output that I got is the maximum value contained just in the last row, I mean, it just considers the output of the last loop and among that last 1x46 row it selects the max value. I need to know the max value considering all the output rows that result from Em and then, the indices, in order to know which theta and Am value are, that produce the maximum energy Em. If you have any advice for this, I would be very grateful.
dpb
dpb el 11 de Ag. de 2018
Editada: dpb el 11 de Ag. de 2018
", I run Em = sum(Gm,1); it works!"
Of course and yes Em is then a row vector; that's what I said...
Em(a,:)=sum(Gm);
will not have an index assignment error unless you've somewhere already defined Em or a is not a valid integer index. I can simulate that operation and demonstrate:
>> for a=1:3 % for a smaller number but same idea over a
Gm=rand(8760,46); % end up having calculated Gm array
Em(a,:)=sum(Gm); % sum and save
end
>> whos Em
Name Size Bytes Class Attributes
Em 3x46 1104 double
>>
and voila! there's the array of sums as expected; no indexing problems.
Would need to see the specific code itself to see where the syntax/logic error lies that created the error, but the concept is valid.
If the question on maxima is to locate the overall maximum in a 2D array, that's doable once you have the 2D array...
[mxEm,imx]=max(Em(:)); % overall maximum, linear index to location
[rmx,cmx]=ind2sub(size(Em),imx); % row,column in 2D array of index to max
This is also easy enough to illustrate...we'll use a smaller sample size so it's easy to see but the principle is the same no matter the size...
>> Em=rand(3,5) % sample made up dataset
Em =
0.8208 0.4135 0.3460 0.3746 0.6936
0.5367 0.5559 0.5161 0.4997 0.2921
0.2624 0.8595 0.7683 0.8453 0.8713
>> [mx,ix]=max(Em(:)) % overall maximum, linear location
mx =
0.8713
ix =
15
>> [r,c]=ind2sub(size(Em),ix) % get row,column associated
r =
3
c =
5
>> Em(r,c) % see got it right...
ans =
0.8713
>>
In the earlier comment, it looked like you were intending to sum down to a single overall total; in that case the position information would have been lost.
Luis Duarte
Luis Duarte el 11 de Ag. de 2018
Editada: dpb el 12 de Ag. de 2018
Thanks a lot for the comments, but seems I haven’t expressed my doubt correctly. Check out the following shorter example, it is very similar to the one I posted in the beginning.
A = [1 2 3 4];
B = [4 3 2 1];
C = [0 1 2 2 0]';
D = [2 3 1 0 1]';
X = rand(5,4);
for i = 1:length(A)
for j = 1:length(B)
FF = C.*A(i)+B(j).*D;
Y = FF+X
G = sum(Y,1)
end
end
[G_max,I]= max(G(:))
[I_row,I_col] = ind2sub(size(G),I)
If you run this code you will see that we got several outputs for G, but when I tried to determine the max value of G, it just take into account the last output of G for the determination of the max value and it does not consider the previous G outputs that result from the loop.
dpb
dpb el 11 de Ag. de 2018
Editada: dpb el 11 de Ag. de 2018
Of course, because in
G = sum(Y,1)
you're overwriting every time instead of saving each sum inside the loop. That's what you were doing in the first pass, too.
As illustrated above, you need something like
...
LA=length(A); LB=length(B);
G=zeros(LA*LB,LA);
k=0;
for i = 1:LA
for j = 1:LB
k=k+1;
FF = C.*A(i)+B(j).*D;
Y = FF+X;
G(k,:)=sum(Y);
end
end
to save the sums if need to keep all for later.
Otherwise, of course, you could simply compute the max for each vector G when compute it and save it similarly excepting for being a vector instead of 2D array. Or, if you're still looking for the overall maximum, then check each in sequence against the previous and save the larger on each pass (as well as its index, of course).
Luis Duarte
Luis Duarte el 12 de Ag. de 2018
Thank yiu so much!, you are totally right. As I am still a beginner using Matlab, I was thinking that the outputs after looping were saved instead of being over written each time. I was thinking that there was no need of using the zeros matrix, but this is a good option, Thanks a lot again!
dpb
dpb el 12 de Ag. de 2018
Editada: dpb el 12 de Ag. de 2018
Matlab is literal in following your source code; you told it to store the sum vector in the variable G so it did--each and every time! :) If there are no indices given on a LH target of an assignment, then the LH becomes the RH result; it doesn't matter what the previous size may have been or content may have been, that is totally overwritten as if had never been.
See Improving Performance; the very first item in the list is preallocation. Undoubtedly the editor suggested there was an issue there before with the orange indicator; it's best to try to make those go away when writing code.
Luis Duarte
Luis Duarte el 12 de Ag. de 2018
Thank you!, I'll follow your suggestion.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Ag. de 2018

Comentada:

el 12 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by