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

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 Comments

This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Thank you Rik. I will follow the tools next time.

Sign in to comment.

 Accepted Answer

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 Comments

% 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);
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{}

Sign in to comment.

More Answers (1)

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 Comments

% The dats showed is generated and saved with numbered variables from a previous work and thats why I don't use arrays.
% In the data first I find a coefficient for each of them and apply that coefficient to get a new corrected data.
% If I write it in a long way for the first data (mean_vec_CO_1) would be like this:
for i=1:length(left)
x = left{i};
y = right{i};
y= [nan(sum(isnan(y)),1);y(~isnan(y))']';
if i == 1
Coefficient_1 = max(mean_vec_CO_1)/max(CO_ppb_Picarro);
else
Coefficient_1= max(CO_ppb_Picarro)/max(mean_vec_CO_1);
end
% and so on for mean_vec_CO_2 and mean_vec_CO_3.
% The secong part for mean_vec_CO_1, in a detailed way would be like this:
mean_vec_CO_1(mean_vec_CO_1>0)=mean_vec_CO_1(mean_vec_CO_1>0)*Coefficient_1;
mean_vec_CO_1(mean_vec_CO_1<0) =mean_vec_CO_1(mean_vec_CO_1<0)*Coefficient_1;
end
% At the end I should have a New_mean_vec_CO_1, New_mean_vec_CO_2, and New_mean_vec_CO_3.
That you got the data in a horrible shape is no excuse to continue the practice. Put the variables in a cell array at the start of your code and use indexed code in your loop. My code should work once you store the variables in a way that makes sense.
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,
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.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 16 Nov 2020

Commented:

Rik
on 16 Nov 2020

Community Treasure Hunt

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

Start Hunting!