Vectorization help, separating an array into multiple arrays
1 回表示 (過去 30 日間)
古いコメントを表示
Hi. I'm having some trouble with vectorization.
Basically I have an excel sheet with data from a weather model. The model outputs data for separate "weather groups" (locations). So first I made some code that finds where each new weather group starts.
if true
num=xlsread('SampleWxData.xlsx');
CurrentWxGroupStart = 1;
AllWxGroupsStart = 1;
while (find(num(:,1)>num(CurrentWxGroupStart,1),1))
NextWxGroupStart = find(num(:,1)>num(CurrentWxGroupStart,1),1);
AllWxGroupsStart = horzcat(AllWxGroupsStart, NextWxGroupStart);
CurrentWxGroupStart = NextWxGroupStart;
end
NumberOfWxGroups = size(AllWxGroupsStart,2);
end
I then read the excel sheet again to get the raw data
if true
[rad,time,all]=xlsread('SampleWxData.xlsx');
end
This part all works. The format of the excel sheet is 3 columns: WxGroup, time, radiation
Now, I want to create a three dimensional array, concatenating each weather groups data. Basically I have a long list of all the data now, and I want to separate it out by the weather group.
I know how to do this with a for loop, but I want to vectorize it.
I know the following can get me the first Weather Group
if true
AllWxGroups = (all(AllWxGroupsStart(1,1):AllWxGroupsStart(1,2)-1,:));
end
So I first added the ending point to the variable AllWxGroupsStart and tried
if true
x=1:size(AllWxGroupsStart,2)-1;
AllWxGroups = all(AllWxGroupsStart(1,x):AllWxGroupsStart(1,x+1)-1,:);
end
But that just returned the first Weather Group again. Not sure why not...I thought putting x there would just do the same thing as before except cycle through all values of x, but I guess not.
Let me know if you need any more clarification.
採用された回答
Sean de Wolski
2013 年 1 月 3 日
編集済み: Sean de Wolski
2013 年 1 月 3 日
Could you provide some small examples of the input variables - my guess is that vectorizing, although possible, will likely be slower than the for-loop due to the intermediate arrays that will be required.
To speed this up, I would recommend preallocating AllWxGroups to be the biggest possible size it will be. Then instead of using cat(3,...), which will be slow do to a memory copy, index into the iith slice, and then remove the rest at the end.
%Pseudoish code
AllWxGroups = zeros(10,10,10);
for ii = 1:10
AllwxGroups(:,:,ii) = your_indexing_expression(ii);
end
2 件のコメント
Sean de Wolski
2013 年 1 月 3 日
編集済み: Sean de Wolski
2013 年 1 月 3 日
Also, depending on what you're doing, this may only take a few seconds so it might not be worth it to spend hours optimizing it.
Good luck!
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Weather and Atmospheric Science についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!