matlab - 在 MATLAB 中的多个循环中从元胞数组中保存、提取相关数据

标签 matlab machine-learning structure signal-processing cell-array

很抱歉问这个问题,这可能是一个简单的问题,但我陷入了困境。它与元胞数组有关。

我有延迟、偏移和阈值。

 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、r、阈值的完整值,如图所示。

延迟=0.0300 偏移=0.0400 J=16 25 24 25 r = 24 21 46 18 阈值 = 0.4:0.9

enter image description here

然后我过滤了J的最小值和r的最大值以及对应于min J和max r的阈值。我已收到如图所示的这些值。

   [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);

enter image description here

对于一种情况,我用红色标记,但我将收到所有循环的这些组合,如整张图片所示。

问题:

• 我想找到延迟、偏移、阈值的组合,其中包含最小J最大r,如图所示我需要的所有组合中的数字我需要包含延迟偏移阈值的组合,其中包含最小J最大 r 。我已将值保存在元胞数组中,因为我对如何从元胞数组中获取组合感到困惑。

• 是否有任何好的方法来保存值或可能的结构(如果是)有人可以解释一下小提示如何工作。

代码:

 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 = q47452082
delay = (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;
  end
end

if 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
  end
else % 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

需要注意的几点:

  • 我删除了一些未使用的变量。
  • 我将 outoutput 预先分配为 struct 数组。
  • 我删除了围绕 J,r 随机化的循环(因为它什么都不做),并将随机数生成移到循环之前(没有理由在每次迭代都可以在开始时一次性完成)。
  • 我使用 dynamic field references 执行分配到两个 struct 数组。
  • 我添加了一个演示,说明如何根据不同的 MATLAB 版本(因此支持的功能不同)运行不同的代码 - 请参阅最后的打印。
  • 如果您不请求输出,该函数只会打印内容。
  • 您的代码可能还有进一步矢量化的空间(即消除循环)。

关于matlab - 在 MATLAB 中的多个循环中从元胞数组中保存、提取相关数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47452082/

相关文章:

matlab - 将8位音频信号转换为较低的编码

python - 如何使用pandas计算最大点击间隔?

规范上下文中的 C 结构大小

c - 为什么编译失败,错误: conversion to non-scalar type requested

c++ - 与 'operator=' 不匹配

MATLAB scatter3、plot3速度差异

Matlab:matlab 如何在函数调用中传递结构?

matlab - 如何实现匹配过滤器

Python 找不到模块 h2o

nlp - 具有 top-k 输出的大规模朴素贝叶斯分类器