Create arrays of observations
1 回表示 (過去 30 日間)
古いコメントを表示
Hello everybody,
Probably it is a very silly question but I am struggling on it..
I have a series of observations saved as 'pt_mean_lev1_1' up to 'pt_mean_lev1_348' (see picture below) and I want to make an array of them being a = 1x348. How can I do that?
Thank you very much!

3 件のコメント
採用された回答
Sulaymon Eshkabilov
2021 年 8 月 4 日
Here is one of the viable solutions with dynamically naming variables and attaining their values from the workspace.
a = [];
for ii=1:348
Var = strcat('pt_mean_lev1_', num2str(ii));
a=[a, eval(Var)];
end
3 件のコメント
Rik
2021 年 8 月 4 日
In the screenshot you can see the number of elements of each element. Growing the array dynamically like this (instead of pre-allocating) results in terrible performance. I would also refrain from teaching people to use eval as the solution. As already evidenced in this thread, that is a source of trouble.
k=500000;N=100;%N=348 takes mare than the 55 second limit
tic
a = [];
for ii=1:N
a=[a, rand(k,1)];
end
toc
tic
a = zeros(k,N);
for ii=1:N
a(:,ii)=rand(k,1);
end
toc
その他の回答 (2 件)
CARLOTTA DENTICO
2021 年 8 月 4 日
1 件のコメント
Sulaymon Eshkabilov
2021 年 8 月 4 日
I meant a separate thread and not here. Two questions in one thread that is confusing.
Rik
2021 年 8 月 4 日
You should not name your variables dynamically. Why are you not storing them in an array when reading? What is wrong with the code below?
lon = ncread('topaz_V4_myocean_arctic_grid1to8_da_class1_19910115.nc', 'longitude');
lat = ncread('topaz_V4_myocean_arctic_grid1to8_da_class1_19910115.nc', 'latitude');
fram = (lon>=0 & lon<=20 & lat>=76 & lat<=80);
pt_lev1_=zeros(size(pt_lev1,1)*size(pt_lev1,2),348);
for k = 1:348
pt_lev1_(:,k) = reshape(pt_lev1(:,:,k),[],1);
end
pt_mean_lev1_=mean(pt_lev1_(fram,1:k),1,'omitnan')
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!