Search code examples
matlabmatrixrangeminimumthreshold

Scan a matrix and find the maximum value in Matlab between 2 ranges


I have a matrix in the form of a text file, I was hoping to scan it using MATLAB, and scan for the maximum value in between 2 points (1.5 and 2) and use that as a threshold.

I wrote a code but it rerturned an error.

    [filename, pathname] = uigetfile('*txt', 'Pick text file');
data = dlmread(fullfile(pathname, filename)); 
t=data(:,1);
N = size(t,1);
m= max(data(1.5,2));
figure;
threshold = m;

Error in file (line 214) m= max(data(1.5,2));


Solution

  • data(1.5,2) does not ask for elements of data with values between 1.5 and 2; it asks for the element of data on the "1.5th" row and 2nd column, which is clearly undefined. Indices have to be integers.

    The elements of data with values between 1.5 and 2 can be obtained with

    data(data > 1.5 & data < 2)
    

    so you can get the largest of these using

    m = max(data(data > 1.5 & data < 2));