本文介绍了使用矢量化或其他方法重新格式化循环-八度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中是否有任何方法可以向量化(或重新格式化)循环的每个主体:

Is there any way to vectorize (or reformulate) each body of the loop in this code:

col=load('col-deau'); %load data

h=col(:,8); % corresponding water column
dates=col(:,3); % and its dates

%removing out-of-bound data
days=days(h~=9999.000);
h=h(h~=9999.000);
dates=sort(dates(h~=9999.000));

[k,hcat]=hist(h,nbin); %making classes (k) and boundaries of classes (hcat) of water column automatically

dcat=1:15; % make boundaries for dates
for k=1:length(dcat)-1 % Loop for each date class
    ii=find(dates>=dcat(k)&dates<dcat(k+1));% Counting dates corresponding to the boundaries of each date class
    for j=1:length(hcat)-1                                % Loop over each class of water column
        ij=find(h>=hcat(j)&h<hcat(j+1)); % Count water column corresponding to the boundaries of each water column class
        obs(k,j)=length(intersect(ii,ij));               % Find the size of each intersecting matrix
    end
end

例如,我尝试使用矢量化来更改此部分:

I've tried using vectorization, for example, to change this part:

for k=1:length(dcat)-1
    ii=find(dates>=dcat(k)&dates<dcat(k+1))
endfor

与此:

nk=1:length(dcat)-1;
ii2=find(dates>=dcat(nk)&dates<dcat(nk+1));

,还使用bsxfun:

and also using bsxfun:

ii2=find(bsxfun(@and,bsxfun(@ge,dates,nk),bsxfun(@lt,dates,nk+1)));

但无济于事.这两种方法都产生相同的输出,并且不对应于使用for循环(就元素和向量大小而言).

but to no avail. Both these approaches produce identical output, and do not correspond to that of using for loop (in terms of elements and vector size).

作为信息,h是一个向量,其中包含以米为单位的水柱,而date是一个向量(具有两位数字的整数),其中包含进行相应水柱测量的日期.输入文件位于以下位置: https://drive.google.com/open?id= 1EomLGYleaNtiGG2iV_9LRt425blxdIsm

For information, h is a vector which contains water column in meters and dates is a vector (integer with two digits) which contains the dates in which the measurement for a corresponding water column was taken.The input file can be found here: https://drive.google.com/open?id=1EomLGYleaNtiGG2iV_9LRt425blxdIsm

对于输出,我想要这样的ii:

As for the output, I want to have ii like this:

ii =

   1177
   1178
   1179
   1180
   1181
   1182
   1183
   1184
   1185
   1186
   1187
   1188
   1189
   1190
   1191
   1192
   1193
   1194
   1195
   1196
   1197
   1198
   1199
   1200
   1201
   1202
   1203
   1204
   1205
   1206
   1207
   1208
   1209
   1210
   1211
   1212
   1213
   1214
   1215
   1216
   1217
   1218
   1219
   1220
   1221
   1222
   1223
   1224
   1225
   1226
   1227
   1228
   1229
   1230
   1231
   1232
   1233
   1234
   1235
   1236
   1237
   1238
   1239
   1240
   1241
   1242
   1243
   1244
   1245
   1246
   1247
   1248
   1249
   1250
   1251
   1252
   1253
   1254
   1255
   1256
   1257
   1258
   1259
   1260
   1261
   1262
   1263
   1264
   1265
   1266
   1267
   1268
   1269
   1270
   1271
   1272

使用第一种方法时,我得到的ii2在值和向量大小方面都非常不同(因为向量大小太大,所以我无法发布结果).

instead with the first approach I get ii2 which is very different in terms of value and vector size (I can't post the result because the vector size is too big).

有人可以在这里帮助绝望的新手吗?我只需要将循环部分重新构造为更好,更简洁的版本.

Can someone help a desperate newbie here? I just need to reformulate the loop part into a better, more concise version.

如果需要添加更多详细信息,请随时问我.

If more details need to be added, please feel free to ask me.

推荐答案

您可以使用 hist3 :

pkg load statistics
[obs, ~] = hist3([dates(:) h(:)] ,'Edges', {dcat,hcat});

这篇关于使用矢量化或其他方法重新格式化循环-八度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:40