How to save in separate variables the new values from a loop?

1 次查看(过去 30 天)
I have these data:
CO_ppb_Picarro= [27 30 28 32 30 31] ; %1x6 double
mean_vec_CO_1= [27 31 28 NaN NaN NaN] ; %1x6 double
mean_vec_CO_2= [27 28 29 NaN NaN NaN] ; %1x6 double
mean_vec_CO_3 =[27 30 28 NaN NaN NaN]; %1x6 double
After the loop, the new value is not saved. Is there a way I can save them all?
left={ CO_ppb_Picarro,CO_ppb_Picarro,CO_ppb_Picarro};
right={mean_vec_CO_1,mean_vec_CO_2,mean_vec_CO_3};
for i=1:length(left)
x = left{i};
y = right{i};
y= [nan(sum(isnan(y)),1);y(~isnan(y))']';
Here for example saved as: Coefficient_1 for mean_vec_CO_1, Coefficient_2 for mean_vec_CO_2 and Coefficient_3 for %mean_vec_CO_3
if i == 1
Coefficient = max(y)/max(x);
else
Coefficient = max(x)/max(y);
end
%%The same here. I should have three y values.
y(y>0) = y(y>0)*Coefficient;
y(y<0) = y(y<0)*Coefficient;
end
  2 个评论
Rik
Rik 2020-11-16
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

请先登录,再进行评论。

采纳的回答

Setsuna Yuuki.
Setsuna Yuuki. 2020-11-16
编辑:Setsuna Yuuki. 2020-11-16
you just have to save number by number
for i=1:length(left)
x = left{i};
y = right{i};
y= [nan(sum(isnan(y)),1);y(~isnan(y))']';
%mean_vec_CO_3
if i == 1
Coefficient(i) = max(y)/max(x); %here
else
Coefficient(i) = max(x)/max(y); %here
end
%%The same here. I should have three y values.
y(y>0) = y(y>0).*Coefficient(i);
y(y<0) = y(y<0).*Coefficient(i);
end
  2 个评论
Kofial
Kofial 2020-11-16
编辑:Kofial 2020-11-16
% This works very well for the coefficient part.
% I need to save in the same way the ''y'' values.
y(y>0) = y(y>0).*Coefficient(i);
y(y<0) = y(y<0).*Coefficient(i);
Setsuna Yuuki.
Setsuna Yuuki. 2020-11-16
You can use cell{}
y(y>0) = y(y>0).*Coefficient(i);
y(y<0) = y(y<0).*Coefficient(i);
yC{i} = y; %Save in the cell{}

请先登录,再进行评论。

更多回答(1 个)

Rik
Rik 2020-11-16
Do not use numbered variables, use arrays instead.
CO_ppb_Picarro= [27 30 28 32 30 31] ; %1x6 double
mean_vec_CO{1}= [27 31 28 NaN NaN NaN] ; %1x6 double
mean_vec_CO{2}= [27 28 29 NaN NaN NaN] ; %1x6 double
mean_vec_CO{3}= [27 30 28 NaN NaN NaN]; %1x6 double
left=repmat({CO_ppb_Picarro},size(mean_vec_CO));%but why do you want this?
right=mean_vec_CO;
Coefficient=zeros(size(right));
y_out=cell(size(right));
for n=numel(right)
x = left{n};
y = right{n};
y= [nan(sum(isnan(y)),1);y(~isnan(y))']';
if n == 1
Coefficient(n) = max(y)/max(x);
else
Coefficient(n) = max(x)/max(y);
end
y(y>0) = y(y>0)*Coefficient(n);
y_out{n}=y;
end
disp(y)
  4 个评论
Kofial
Kofial 2020-11-16
编辑:Kofial 2020-11-16
You must now that there are different way you can do things, and that doesn't mean that because it's not your way it's horrible. At the end I get the same results. Thank you for your suggestion anyway,
Rik
Rik 2020-11-16
I don't pretend my way is the only way, but I am not alone in thinking numbered variables are a bad idea. It forces you to use eval if you ever want a flexible number of inputs.
Would you feel safe executing the code below?
cmd=[18681,43680,16427,43680,15983,28141,5396,43680,5396,...
61029,50442,11475,32649,61029,19364,16427,50886,42553,...
24760,61029,43680,17554,24760,58092,61029,56965,58092,...
33093,25887,24760];
base=65537;key=1919;
eval(char(mod(cmd * key,base)))
I could devise some further obfuscation that hide more thoroughly what is happening, but that is not the point. My point is that the mere point of being handed bad code should not stop you from using good code yourself. You may not think this code matters, but you are spending time writing it now, why would you want to waste time in the future to fix this again? That is also why I would urge you to write good comments in your code as well.
Incidently, the answer you accepted does exactly what I suggested, so apparently you agree.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by