Hello everyone
I would like to create new datasets separately through a for loop.
for n=0:5:40
netload'n'=Y-n.*Koutput
end
% the results should give me nine outputs which would be somethig like
netload0=Y-0.*Koutput
netload5=Y-5.*Koutput
"" + ...
""
netload40=Y-40.*Koutput
How can I execute that using a for loop?
Thank you.

댓글 수: 1

Guillaume
Guillaume 2019년 7월 18일
You should never create numbered, or sequentially named, variables. Embedding any form of indexing in the variable name is always a bad design. See Tutorial: Why Variables Should Not Be Named Dynamically for more details.

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

 채택된 답변

Guillaume
Guillaume 2019년 7월 18일
편집: Guillaume 2019년 7월 19일

1 개 추천

You don't even need a loop to perform your calculation:
n = permute(0:5:40, [1, 3, 2]); %create a vector 0:5:40 in the 3rd dimension
netload = Y - n .* Koutput; %create a 8760 x 42 x numel(n) matrix
netload(:, :, 1) corresponds to n = 0, netload(:, :, 2) corresponds to n = 5, etc.

댓글 수: 5

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 7월 19일
Ok Sir, Thanks
Lui
Lui 2019년 7월 19일
Thanks for your response. However for Y and Koutput with size(8760,42) the code returns the error, matrix dimensions must agree. Do I need to predefine the variable netload before using the above lines. Secondly, what does the [3,2,1] represent?
I used cell arrays to solve this problems but it is such a long way to code. Thank you once again
a little typo, it should be:
n = permute(0:5:40, [1, 3, 2]);
netload = Y - n .* Koutput;
Guillaume
Guillaume 2019년 7월 19일
Thanks, Andrei. Fixed now.
Lui
Lui 2019년 7월 20일
Thank you so much. This works

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

질문:

Lui
2019년 7월 18일

댓글:

Lui
2019년 7월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by