抱歉问一个可能很简单的问题,但我在这上面卡住了。这与单元数组有关。
我有 delay, offset 和 threshold
。
delay = [0.01:0.01:0.03]; offset = [0.02:0.01:0.04]; threshold = [0.4:0.1:0.9]; limit_for_idx = [0.4:0.1:0.9]; limit = [0.4:0.1:0.9];
我尝试提供一个示例来询问我遇到问题的一部分。
在循环的第一部分中,我收到了 Delay, Offset, J, r, threshold 的完整值,如图所示,适用于所有循环。
delay =0.0300
offset=0.0400
J=16 25 24 25
r = 24 21 46 18
threshold = 0.4:0.9
然后我过滤了 J 的最小值和 r 的最大值,以及对应于最小 J 和最大 r 的 threshold 值。我收到了如图所示的这些值。
[min_J,min_J_loc] = min(J(:)) [max_r,max_r_loc] = max(r(:)) thresh_min_J = threshold(min_J_loc); thresh_max_r = threshold(max_r_loc);
对于一个案例,我用红色标记了,但我会收到所有循环的这些组合,如整个图片所示。
问题:
• 我希望找到包含最小 J
和最大 r
的delay, offset, threshold
组合,如图所示,从所有组合中,我需要包含最小 J
和最大 r
的delay
、offset
、threshold
组合。我已经将值保存在单元数组中,但我不知道如何从单元数组中提取组合。
• 是否有更好的方法来保存这些值,或者使用结构体?如果有的话,有人能解释一下吗?哪怕是一个小提示也行。
代码:
delay = [0.01:0.01:0.03]; offset = [0.02:0.01:0.04]; threshold = [0.4:0.1:0.9]; limit_for_idx = [0.4:0.1:0.9]; limit = [0.4:0.1:0.9]; J=0; T = 1; b=1; K=1; for H = 1:numel(delay) for G = 1:numel(offset) for R = 1:numel(threshold); J = randi([10 25],1,4); r = randi([10 50],1,4); end [min_J,min_J_loc] = min(J(:)) [max_r,max_r_loc] = max(r(:)) thresh_min_J = threshold(min_J_loc); thresh_max_r = threshold(max_r_loc); out{K,:} = [ delay(H) offset(G) J r threshold]; output{T,:} = [delay(H) offset(G) min_J max_r thresh_min_J thresh_max_r]; K=K+1; T = T+1; end end for X = 1:numel(out) disp(' delay , offset(G) , J, r , threshold ') Q = out{X}; disp(Q) end for X = 1:numel(output) disp(' delay , offset(G) , min_J, max_r , thresh_min_J thresh_max_r ') Z = output{X}; disp(Z) end
回答:
请查看这个修改后的示例版本:
function varargout = q47452082delay = (0.01:0.01:0.03);offset = (0.02:0.01:0.04);threshold = (0.4:0.1:0.9);nD = numel(delay);nO = numel(offset);J = randi([10 25],1,4,nD,nO);r = randi([10 50],1,4,nD,nO);%% Preallocate%{The simplest way to do it:out = NaN(nD*nO,3*1+2*4);output = NaN(nD*nO,6);%}out = struct('delay',[],'offset',[],'J',[],'r',[],'threshold',[]);out = repmat(out,nD*nO,1);output = struct('delay',[],'offset',[],'min_J',[],'max_r',[],... 'thresh_min_J',[],'thresh_max_r',[]);output = repmat(output,nD*nO,1);fn{2} = fieldnames(output);fn{1} = fieldnames(out);%% Populate the data structures:K = 1;for H = 1:numel(delay) for G = 1:numel(offset) [min_J,min_J_loc] = min(J(:,:,H,G)); [max_r,max_r_loc] = max(r(:,:,H,G)); thresh_min_J = threshold(min_J_loc); thresh_max_r = threshold(max_r_loc); data = {delay(H),offset(G),J(:,:,H,G),r(:,:,H,G),threshold}; for indF = 1:numel(fn{1}) out(K).(fn{1}{indF}) = data{indF}; end data = {delay(H), offset(G), min_J, max_r, thresh_min_J, thresh_max_r}; for indF = 1:numel(fn{2}) output(K).(fn{2}{indF}) = data{indF}; end K = K+1; endendif nargout == 0 % if no outputs requested, print if ~verLessThan('matlab','8.2') % tables exist in MATLAB R2013b or newer disp(struct2table(out)); disp(struct2table(output)); else for X = 1:numel(out) Q = out(X); disp(Q) end for X = 1:numel(output) Z = output(X); disp(Z) end endelse % otherwise output the desired data: % OPTION #1: separate variables % You should call the function like so: [min_J_cases,max_r_cases] = q47452082(); varargout{1} = output([output.min_J] == min([output.min_J])); varargout{2} = output([output.max_r] == max([output.max_r])); % OPTION #2: 1 output, 2x1 cell %{ varargout = {output([output.min_J] == min([output.min_J]));... output([output.max_r] == max([output.max_r]))}; %} % OPTION #3: 1 output, 2x1 struct %{ varargout = {[output([output.min_J] == min([output.min_J]));... output([output.max_r] == max([output.max_r]))]}; %}end
你应该注意以下几点:
- 我删除了一些未使用的变量。
- 我预分配了
out
和output
作为struct
数组。 - 我删除了
J
、r
随机化的循环(因为它没有任何作用),并将随机数生成移到了循环之前(没有理由在每次迭代中都做这件事,可以一次性在开始时完成)。 - 我使用 动态字段引用 对两个
struct
数组进行赋值。 - 我添加了一个演示,展示如何根据不同的 MATLAB 版本(因此是不同的支持功能)运行不同的代码 – 请参见结尾的打印部分。
- 如果您没有请求输出,函数只会打印内容。
- 您的代码可能还有进一步向量化的空间(即去掉循环)。