필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Kofial
Kofial 2020년 11월 16일
댓글: Rik 2020년 11월 16일
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.
Kofial
Kofial 2020년 11월 16일
Thank you Rik. I will follow the tools next time.

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by