【问题标题】:Code to compare cells with a range in MATLAB将单元格与 MATLAB 中的范围进行比较的代码
【发布时间】:2018-03-12 13:21:50
【问题描述】:

我正在尝试编写一个可以将单元格与范围进行比较的代码。在这种情况下,我有一个代码,它取特定范围内的一组数据的平均值,然后当它完成后,我希望它比较单元格以查看单元格中的值是否在范围内,以及它是否更小平均值,如果不显示 2,则显示 1。 例如我有这样的数据:

0.1 , 0.6 , 1.1 , 0.15 , 0.55

首先,我取 0.5、1、1.5 之间的数字的平均值。以上数字的平均值为:

range    average
 0.5       0.125
 1.0       0.575
 1.5       1.1

然后,我想比较单元格,如果第一个值介于 0 和 0.5 之间并且小于其范围的平均值,则显示 1,如果大于平均值则显示 2。如果介于 0.5 和 1 之间并且小于其范围的平均值,则显示 3,如果大于则显示 4,依此类推。所以,上面显示的数字将制作这样的地图:

  1 , 4 , 5 , 2 , 3

我的代码是:

% Indexes
row = 1;
col = 1;
range = 1;
% Constants
MAX_RANGE = 5;
MAX_DISPLAY_VALUE = 10;
HIGH_ERROR = 2.5;
LOW_ERROR = 0.0;
% Range Value variables
rangeValue = zeros(1, MAX_RANGE);
rangeTotalForAverages = zeros(1, MAX_RANGE);
rangeAveragesCount = zeros(1, MAX_RANGE);
rangeCount = zeros(1, MAX_RANGE);
rangeAverage = zeros(1, MAX_RANGE);
% Display Value Varibales
displayValue = zeros(MAX_DISPLAY_VALUE);
displayValueCount = zeros(MAX_DISPLAY_VALUE);
% Set up the range bounds
rangeValue(1) = 0.5;
rangeValue(2) = 1.0;
rangeValue(3) = 1.5;
rangeValue(4) = 2.0;
rangeValue(5) = 2.5;
% Read the file
rawData = dlmread('data_1.csv',',');
% Get the size
[MAX_ROWS, MAX_COLS] = size(rawData);
% Map arrays
errorMap = double(zeros(MAX_ROWS, MAX_COLS));
classificationMap = double(zeros(MAX_ROWS, MAX_COLS));
% A function to round up 
value = ceil(rawData(row, col)*1000)/1000;
% Print the raw data
fprintf('Raw Data\n');
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
    fprintf('%0.3f ', rawData(row, col));
end
fprintf('\n');
end 
% Print the Error Map
fprintf('Error Map\n');
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
    if rawData(row, col) > HIGH_ERROR
        errorMap(row, col) = rawData(row, col);
        rawData(row, col) = HIGH_ERROR;
        if rawData(row, col) < LOW_ERROR
        errorMap(row, col) = rawData(row, col);
        rawData(row, col) = LOW_ERROR;   
        end
    end
fprintf('%0.3f ', errorMap(row, col));
end
fprintf('\n');
end
% Print the Rounded Data
fprintf('Rounded Data\n');
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
    value = ceil(rawData(row, col)*1000)/1000;
    fprintf('%0.3f ', value);
end
fprintf('\n');
end
% Calculate and store the averages for each range
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
  value = ceil(rawData(row, col)*1000)/1000; 
  for range = 1 : MAX_RANGE
    if value <= rangeValue(range)
        rangeTotalForAverages(range) = rangeTotalForAverages(range) + value;
        rangeAveragesCount(range) = rangeAveragesCount(range) + 1;
        rangeCount(range) = rangeCount(range) + 1;  
        break
    end
  end
end   
end
for range = 1 : MAX_RANGE
if rangeAveragesCount(range) > 0
    rangeAverage(range) = rangeTotalForAverages(range) / 
rangeAveragesCount(range);
end
end
% Print the average
fprintf('Average\n');
for range = 1 : MAX_RANGE
fprintf('%0.3f ', rangeAverage(range));
fprintf('\n');
end
% Create the Classification Map
tempDisplayValueIndex = 0;
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
  value = ceil(rawData(row, col)*1000)/1000;
  for range = 1 : MAX_RANGE
      % If the value is in the range
   if value <= rangeValue(range) 
       % If the value is less than the average for the range, set the 
 display value
   tempDisplayValueIndex = range * 2;
   if value <= rangeAverage(range)
   classificationMap(row, col) = displayValue(tempDisplayValueIndex);
   displayValueCount(tempDisplayValueIndex) = 
displayValueCount(tempDisplayValueIndex) + 1;
   else
       % Store the classification map and count the display value
   classificationMap(row, col) = displayValue(tempDisplayValueIndex + 1); 
   displayValueCount(tempDisplayValueIndex) = 
displayValueCount(tempDisplayValueIndex) + 1;
   break;
   end
   end
  end
end
end
% Print the Classification Map
fprintf("\n Classification Map \n");
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
fprintf('%f ', classificationMap(row, col)); 
end
fprintf('\n');
end

它只为所有值打印零! 任何帮助都会得到很大的帮助。

【问题讨论】:

    标签: matlab classification


    【解决方案1】:

    如果没有 .csv 文件,我无法确定,但让我们看一下:你有一个 classificationMap-元素始终为零。

    如果您查看您的代码,您填充此矩阵的行似乎是

    classificationMap(row, col) = displayValue(tempDisplayValueIndex);
    

    classificationMap(row, col) = displayValue(tempDisplayValueIndex + 1);
    

    那么你在哪里将值放入 displayValue 中?在您的代码中尝试 CTRL+F,分配此变量时的另一行我发现只有这一行

    displayValue = zeros(MAX_DISPLAY_VALUE);
    

    所以这个变量在开始创建时用零填充,以后不再修改。

    我认为,如果您的 FOR 循环适用于 value,答案可能是

    classificationMap(row, col) = value
    

    (如果不尝试代码,我无法确定)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      相关资源
      最近更新 更多