How to save in separate variables the new values from a loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Kofial
el 16 de Nov. de 2020
Comentada: Rik
el 16 de Nov. de 2020
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 comentarios
Rik
el 16 de Nov. de 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Respuesta aceptada
Setsuna Yuuki.
el 16 de Nov. de 2020
Editada: Setsuna Yuuki.
el 16 de Nov. de 2020
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 comentarios
Setsuna Yuuki.
el 16 de Nov. de 2020
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{}
Más respuestas (1)
Rik
el 16 de Nov. de 2020
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 comentarios
Rik
el 16 de Nov. de 2020
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.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!