matrix creation from a loop

Hello all,
I would like to create 60 vector columns of zeros (6964,1) called lat_01_days, lat_02days, lat_03_days until 60. I know I could write them one by one but if i know How to create a loop to create them it would be better also for future research. Can someone help mep lease?
regards
Jonathan

2 件のコメント

Stephen23
Stephen23 2020 年 9 月 17 日
"I would like to create 60 vector columns of zeros (6964,1) called lat_01_days, lat_02days, lat_03_days until 60."
Numbering variables like that is a sign that you are doing something wrong.
Forcing meta-data (e.g. pseudo-indices) intoe variable names is a sign that you are doing something wrong.
"I know I could write them one by one ..."
Ouch!
"...but if i know How to create a loop to create them it would be better also for future research"
In fact what "would be better also for future research" is to avoid numbered variables entirely:
Jonathan Demmer
Jonathan Demmer 2020 年 9 月 17 日
Thank you very much.

サインインしてコメントする。

回答 (3 件)

Walter Roberson
Walter Roberson 2020 年 9 月 17 日

2 投票

if i know How to create a loop to create them it would be better also for future research.
Our long experience is that it would make your future research worse instead of better.

1 件のコメント

Jonathan Demmer
Jonathan Demmer 2020 年 9 月 17 日
Thank you very much

サインインしてコメントする。

Stephen23
Stephen23 2020 年 9 月 17 日

1 投票

The simplest and most efficient solution is to just create one matrix:
lat = zeros(6964,60)
which you can then trivially access using indexing. There is no point in making it more complex than that.
Ruger28
Ruger28 2020 年 9 月 17 日
編集済み: Ruger28 2020 年 9 月 17 日

0 投票

The two other answers are a more correct way of doing it. There is no need to create this many variables in your workspace. However, if you must...
You should use a structure instead of individual variables.
Blank_Vec = zeros(6964,1);
Num_Cols = 60;
for ii = 1:Num_Cols
name = sprintf('lat_%i_days',ii);
ZeroColStruct.(name) = Blank_Vec;
end
if you absolutely MUST have individual variables, one method is this:
% Removed due to being a terrible way to do something

3 件のコメント

Stephen23
Stephen23 2020 年 9 月 17 日
Do NOT follow this advice. It teaches a complex and inefficient use of MATLAB.
"However, if you must..."
"if you absolutely MUST have individual variables..."
Most of the situations that I have read on this forum where someone explains why they "must" use dynamic variable names it turns out to be simpler and more efficient to solve using other approaches. A not-very-well-informed-user's idea of "must" is unlikely to be very correct, based on many years of threads on this forum.
For basic tasks like the one shown in this question, I have yet to see any situation that is not better solved using the recommended approach of basic MATLAB indexing.
Jonathan Demmer
Jonathan Demmer 2020 年 9 月 17 日
I am kind of new i understand what everybody else is saying above and i will follow your guidelines. Thank you very much.
Jonathan Demmer
Jonathan Demmer 2020 年 9 月 17 日
Thank you Stephen for your advice.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2020 年 9 月 17 日

編集済み:

2020 年 9 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by