Extract variable in nested for loop that otherwise gets replaced

1 次查看(过去 30 天)
I have several for loops that look something like this
p=rand(....)
h=rand(....)
for i:100
HCPV(i)=p(i)*h(i)
n=29
for j=1:n
cum_CO2inj(j)=function(p(i), HCPV(i),j) %I simplify here this is not actually the full code
%I want to generate/save cum_CO2inj here (which is a vector of length 30) for every iteration of i (outer most loop)?
end
end
cum_CO2inj otherwise just rewrites it self every time we iterate over i - instead I would like to see the output of 100 cum_CO2inj vectors of size 30 or a matrix of size 100 by 30? Can anyone help me with this please?

采纳的回答

John BG
John BG 2016-9-28
1.- the product
HCPV(i)=p(i)*h(i)
need not be in a for loop. If you define p and h same size, in MATLAB you simply do element wise product, like this:
HCPV=p.*h
add ; at the end of each command line if processing large inputs, to suppress command line echo, that is most of time slower than the processing itself
2.- you are right regarding the line
for j=1:n
cum_CO2inj(j)=function(p(i), HCPV(i),j)
..
simply repeats 100 times because of the unnecessary first for loop.
3.- so you say you want
output .. or a matrix of size 100 by 30
let's allocate space
n=30;
cumCO2inj=zeros(100,n);
one way of doing it would be
for k=1:1:100
cum_CO2inj(k,:)=function2(p(k),HCPV(k))
end
where function2 already takes into account the length n=30 or n=29, doesn't seem that there is neither need here for the input j
Show me the function and I will help you modify it to avoid having to input your vector index j.
Another way would be with bsxfun
C=bsxfun(fun,A,B)
where A and B are input matrices of same size, and fun is either a built-in or custom function that processes element wise each pair of elements of A and B.
bsxfun built-in functions
if your function is not any of the built-in basic ones listed in this table, you can define a custom function for bsxfun for instance like this:
function2 = @(A,B) A.*sin(B);
A = 1:7;
B = pi*[0 1/4 1/3 1/2 2/3 3/4 1].';
C = bsxfun(function2,A,B)
or you can put your function2 in a separate file named function2.m
then you would have something like this
for k=1:1:100
cum_CO2inj(k,:)=bsxfun(function2,p,HCPV)
end
All above said helps but still doesn't solve the main problem:
4.- there would still be 100 times the same processed result, because same apparently randomly generated vectors are used in all 100 outputs.
This happens because you do not include the random generation inside the for loop that generates cum_CO2inj.
Try this
function2 = @(A,B) A.*sin(B); % whatever
for k=1:1:100
p=rand(..)
h=rand(..)
HCPV=p.*h
cum_CO2inj(k,:)=function2(p,HCPV)
end
or
function3 = @(A,B) A.*sin(B);
for k=1:1:100
p=rand(..)
h=rand(..)
HCPV=p.*h
cum_CO2inj(k,:)=bsxfun(function2,p,HCPV)
end
or
p=randi([-10 10],1,10,100)
h=randi([-10 10],1,10,100)
HCPV=p.*h
cum_CO2inj=bsxfun(function3,p,HCPV)
In the more compact last option function3 may be quite the same as function2 or your original function, but it has to take into account that you have already generated all 100 random vectors, so there is no need for any for loop at all here.
I say again, if you show the function you want to implement I will check it and modify accordingly to make it work with the changes here mentioned.
Please if you find my answer of any help would you be so kind to mark my answer as accepted answer?
thanks in advance.
To any other reader, if you find my answer of any help would you please give it a thumbs-up click? thanks in advance.
Appreciating time and attention
John BG

更多回答(1 个)

Thorsten
Thorsten 2016-9-28
编辑:Thorsten 2016-9-28
p=rand(....)
h=rand(....)
HCPV=p.*h;
for i = 1:100
for j = 1:29
cum_CO2inj(i,j) = function(p(i), HCPV(i), j);
end
end
Of course there might be further possible optimisations, as John BG suggests. You can vectorise your function such that you can ultimately write
cum_CO2inj = function(p, HCPV, 1:29);
It may be clearer, depending on your further tasks, to rewrite your function to take p and h as inputs, rather than HPCV that containes and compute HCPV inside your function:
cum_CO2inj = function(p, h, 1:29);

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by