How to calculation the difference of a number in a column?

4 views (last 30 days)
Hi everyone,
May someone help me..
I have data in multiple columns. That is required to compare with another column in such a way that the difference from the highest and the lowes value is calculted as presnted below:
For example, in case of 22, we lsearch in the first column there is only 9 so we place NaN , then we move forward, In column 2 we have 20 and 31, so we calcualte the diffrenec of 22 with its imigiate higher and lower entry that is 9 and 2 then we calculate 9/(9+2)... and so one for rest of the data.

Answers (1)

Samir Mitha
Samir Mitha on 12 Sep 2020
If your data is from a spreadsheet you can import it into MATLAB.
You can then convert the data to numbers using this.
I wrote some code for the proof of concept for column 4, using 22 as the test number with comments.
There are a variety of ways in which this can be done. The method I used inserts the test number at end of the array that contains the column data and sorts it. It then takes the previous and next numbers in the array and subtracts them, which returns the desired value. If the test number is at the beginning or the end of the column, then it will return 'NaN'.
col1 = [9];
col2 = [20, 31];
col3 = [8, 41, 45, 53, 81];
col4 = [2, 7, 11, 19, 25, 31, 42, 56]; % this column used in test
col5 = [14, 16, 29, 31, 38, 45, 60];
test1 = [22, 30, 35]; % 22 is the first index of the test array
col = cat(2, col4, test1(1)); % this adds 22 to the end of the array
col_sorted = sort(col); % sorts the array
index = find(col_sorted == test1(1)); % this returns the index of 22 in the array
if((index == 1) || (index == length(col_sorted))) % checks if 22 is the highest or lowest number
result = NaN;
else
lower_index = index - 1; % returns index of lower number
upper_index = index + 1; % returns index of higher number
higher = col_sorted(upper_index) - col_sorted(index); % calculates difference of higher - 22
lower = col_sorted(index) - col_sorted(lower_index); % calculated difference of 22 - lower
result = higher/(higher+lower);
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!