简体   繁体   English

MATLAB:遍历多个双 arrays 以找到最小值/最大值

[英]MATLAB: Iterating through multiple double arrays to find min/max value

I'm currently trying to find the min/max value of 5 different 2x1 arrays of doubles on Matlab .我目前正在尝试在 Matlab 上找到 5 个不同的 2x1 arrays双打的最小值/最大值。 I wrote a function below that works, returning the min and max values from all 5 arrays listed.我在下面写了一个 function ,它返回所有列出的 5 个 arrays 的最小值和最大值。 However, it looks so long and bulky.但是,它看起来又长又笨重。 I was wondering if there was a more efficient way of doing this?我想知道是否有更有效的方法来做到这一点?

I was thinking of making an array of arrays, but I don't want to copy all that data and waste memory just to check for a min and max value.我正在考虑制作一个 arrays 数组,但我不想复制所有数据并浪费 memory 只是为了检查最小值和最大值。

Any ideas on this?对此有什么想法吗? Thanks!谢谢!

%% Finds min/max Lat values
function results = FindRanges(hObject,handles)

% Min of Latitudes
minLat = TG_Lat;
if min(handles.AC_Lats(:)) < minLat
    minLat = min(handles.AC_Lats(:));
end
if min(handles.MS_Lats(:)) < minLat
    minLat = min(handles.MS_Lats(:));
end
if min(handles.DL_Lats(:)) < minLat
    minLat = min(handles.DL_Lats(:));
end
if min(handles.PI_Lats(:)) < minLat
    minLat = min(handles.PI_Lats(:));
end
if min(handles.EM_Lats(:)) < minLat
    minLat = min(handles.EM_Lats(:));
end

% Max of Latitudes
maxLat = TG_Lat;
if max(handles.AC_Lats(:)) > maxLat
    maxLat = max(handles.AC_Lats(:));
end
if max(handles.MS_Lats(:)) > maxLat
    maxLat = max(handles.MS_Lats(:));
end
if max(handles.DL_Lats(:)) > maxLat
    maxLat = max(handles.DL_Lats(:));
end
if max(handles.PI_Lats(:)) > maxLat
    maxLat = max(handles.PI_Lats(:));
end
if max(handles.EM_Lats(:)) > maxLat
    maxLat = max(handles.EM_Lats(:));
end

results = [minLat, maxLat];

Copying 5 2x1 arrays is no big deal.复制 5 2x1 arrays 没什么大不了的。 That might even be more efficient than calling the max and min function 5 times (function call overhead is a real thing in MATLAB still).这甚至可能比调用maxmin function 5 次更有效(函数调用开销在 MATLAB 中仍然是真实的)。 So you could do:所以你可以这样做:

data = [handles.AC_Lats(:); handles.MS_Lats(:); handles.DL_Lats(:); ...
        handles.PI_Lats(:); handles.EM_Lats(:)];
results = [min(data), max(data)];

If arrays are ( potentially ) larger, you could do this:如果 arrays (可能)更大,您可以这样做:

minLat = min([min(handles.AC_Lats(:)), min(handles.MS_Lats(:)), min(handles.DL_Lats(:)), ...
              min(handles.PI_Lats(:)), min(handles.EM_Lats(:))]);
maxLat = max([max(handles.AC_Lats(:)), max(handles.MS_Lats(:)), max(handles.DL_Lats(:)), ...
              max(handles.PI_Lats(:)), max(handles.EM_Lats(:))]);
results = [minLat, maxLat];

This is the same thing you do, but using the built-in min and max functions instead of writing them out.这与您所做的相同,但使用内置的minmax函数而不是将它们写出来。

PS: I've ignored your TG_Lat , not sure what that is used for. PS:我忽略了您的TG_Lat ,不确定它的用途。 Currently it looks like a 6th data element, but it is not described in the question text.目前它看起来像第 6 个数据元素,但在问题文本中没有描述。

My current answer assumes what I believe to be the case regarding your handles object, ie that it only contains the information relevant to this task.我目前的答案假设我认为您的句柄 object 是这种情况,即它仅包含与此任务相关的信息。 If this is not the case then this will not work as it iterates through all the properties of the handles object.如果不是这种情况,那么这将不起作用,因为它会遍历句柄 object 的所有属性。

data = [TG_Lat,structfun(@(x)x(1),handles)';TG_Lat,structfun(@(x)x(2),handles)'];
results = [ min(min(data)),max(max(data)) ];

I would like to also add that, although there is nothing wrong with code being a bit bulky, you make too many calls to min() and max() .我还想补充一点,虽然代码有点笨重并没有错,但你对min()max()的调用太多了。 Vectorization aside (which will likely not substantially change performance in this case), it seems like a good practice to avoid calling the same function twice for the same result (in the if and in the assignment).除了矢量化(在这种情况下可能不会显着改变性能),避免为相同的结果(在if和赋值中)调用相同的 function 两次似乎是一个好习惯。

Edit 1 Actually, it seems struct2array would be even simpler, and looking at Cris Luengo's answer putting the data in one dimension would work as well, so here's another answer that uses some of their answer;) (I hope this is cool.)编辑 1实际上,似乎 struct2array 会更简单,并且查看 Cris Luengo 将数据放在一维中的答案也可以,所以这是另一个使用他们的一些答案的答案;)(我希望这很酷。)

data = [TG_Lat([1;1]),struct2array(handles)];
results = [min(data(:)),max(data(:))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM