用于在MATLAB

时间:2017-10-01 04:10:34

标签: matlab classification

我正在尝试编写一个可以将单元格与范围进行比较的代码。在这种情况下,我有一个代码,它取一组特定范围内的数据的平均值,然后当它完成时,我希望它比较单元格,看看单元格中的值是否在范围内,如果它是小于平均值,如果不显示则显示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

它只为所有值打印零! 任何帮助都会被挪用。

1 个答案:

答案 0 :(得分:0)

如果没有.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

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