MATLAB Answers

Code to compare cells with a range in MATLAB

5 views (last 30 days)
Nima Rahmani Mehdiabadi
Nima Rahmani Mehdiabadi on 1 Oct 2017
Edited: Stephen Cobeldick on 1 Oct 2017
I am trying to write a code which can compare a cell with a range. In this case, i have a code which takes average of a set of data within specific ranges and then when it is done i want it to compare the cells to see if the value in the cell is within the range and if it is smaller that the average , then display 1 if not display 2. For example i have a data like this:
0.1 , 0.6 , 1.1 , 0.15 , 0.55
First i take averages of numbers between 0.5 , 1, 1.5. Average of the numbers above are:
range average
0.5 0.125
1.0 0.575
1.5 1.1
Then, i want to compare the cells, if the first value is between 0 and 0.5 and smaller than the average of its range, show 1 and if it is greater than its average show 2. if it is between 0.5 and 1 and smaller than the average of its range, show 3 and if greater show 4 and so one.So, the numbers shown above would make a map like this:
1 , 4 , 5 , 2 , 3
My code is :
% 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;
end
break;
end
end
end
end
% Print the Classification Map
fprintf("\nClassification Map\n");
for row = 1 : MAX_ROWS
for col = 1 : MAX_COLS
fprintf('%f ', classificationMap(row, col));
end
fprintf('\n');
end
It only prints zeros for all the values in the map!!

Answers (2)

Stephen Cobeldick
Stephen Cobeldick on 1 Oct 2017
Edited: Stephen Cobeldick on 1 Oct 2017
There is no point in writing MATLAB code like complex C++ code, with lots of loops and ifs. MATLAB code should be elegant, clear, and neat.
>> V = [0.1,0.6,1.1,0.15,0.55]; % values
>> E = 0:0.5:1.5; % bin edges
>> [~,X] = histc(V,E);
>> M = accumarray(X(:),V(:),[],@mean)
M =
0.12500
0.57500
1.10000
>> E(2,1:end-1) = M;
>> sum(bsxfun(@lt,E(1:end-1).',V),1)
ans =
1 4 5 2 3

Yogananda Jeppu
Yogananda Jeppu on 1 Oct 2017
Never use Matlab like a C code interpreter. Matlab has elegant function that help you do data manipulation very easily. You can easily do this in an array of numbers (see help cell2mat). Once you have an array of numbers use the find command to find indices or rows where the value is within range. I use this as ii = find( A > 0.5 && A <= 1); ii will have all the indices where this is true. Take the average. In the new index find above and below this average using find again. 'find' is very very powerful. Use it.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!