How to find a corresponding value in a table in Matlab - matlab

In Matlab, I am given a table with two columns. Now I want to find the corresponding value of the left column to the maximum value of the right column:
P1_sat = P1(ismember(P2,max(P2)))
This works, however, the maximum is identical at 3 values of the left column, P1. These 3 values are right next to each other. So I want to consider the mid value. Is there a "consider the mid value" - command?

Adding the following code line will complete my goal:
P1_sat = sum(P1_sat,1) / length(P1_sat)
The three different values of P1, whose corresponding value of P2 is the maximum of P2, are added and then divided by 3. This gives the average value, which is also the mid point.

Related

TSQL Summing to a value

I am looking to take a table like the one below. Code is a part that exists on a job, and Value are the different cut lengths of that part needed. Ultimately, I want to sum as many of these cuts that I can get out of a full bar which would equal 1.
Code Value
X 0.10000000
X 0.40000000
X 0.64000000
X 0.30000000
X 0.52000000
Expected End Results is
Code Value
X .8
X .64
X .52
I am not sure the best way of saying it but I want to sum all rows grouping by the Part Code and return as many results of the Sum that do not exceed my predetermined value of 1...

MATLAB Extracting Column Number

My goal is to create a random, 20 by 5 array of integers, sort them by increasing order from top to bottom and from left to right, and then calculate the mean in each of the resulting 20 rows. This gives me a 1 by 20 array of the means. I then have to find the column whose mean is closest to 0. Here is my code so far:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
MeanArray= mean(transpose(NewArray(:,:)))
X=min(abs(x-0))
How can I store the column number whose mean is closest to 0 into a variable? I'm only about a month into coding so this probably seems like a very simple problem. Thanks
You're almost there. All you need is a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
ColNum = find(abs(mean(NewArray,1))==min(abs(mean(NewArray,1)))); %// gives you the column number of the minimum
MeanColumn = RandomArray(:,ColNum);
find will give you the index of the entry where abs(mean(NewArray)), i.e. the absolute values of the mean per column equals the minimum of that same array, thus the index where the mean of the column is closest to 0.
Note that you don't need your MeanArray, as it transposes (which can be done by NewArray.', and then gives the mean per column, i.e. your old rows. I chucked everything in the find statement.
As suggested in the comment by Matthias W. it's faster to use the second output of min directly instead of a find:
RandomArray= randi([-100 100],20,5);
NewArray=reshape(sort(RandomArray(:)),20,5);
% MeanArray= mean(transpose(NewArray(:,:))) %// gives means per row, not column
[~,ColNum] = min(abs(mean(NewArray,1)));
MeanColumn = RandomArray(:,ColNum);

Display multiple values in one line

I'm new to MATLAB. I have two values x and y. Both of them contain values with unknown accuracy. The problem: How could I display them both in one row with 2 digits after comma? Like:
x<tabulation or stack of spaces>y<then goes new line>
Example
RAW data
0,324352 0,75234
1,563 3,4556
Expected output
0,34 0,75
1,56 3,45
Upd: for one value it works well
disp('x=' num2str(x,3));
Purpose is: display TWO values on one row with the new line symbol
The answer is:
disp(num2str([x y],3));
The 3 value means - max.quanity of symbols after comma including it(am I wrong? just thoughts)
Another Idea:
Somehow represent X and Y as array values and then display them.

Taking average of one column with w.r.to other column

I have two columns in .std file. I want average of the second column values corresponding to all values ranging from some value (eg. 1.0- 1.9) in first column how can I program in Matlab?
Say, a is the name of your two column matrix. If you want to find all of the values in the first column in the range of 1.0 - 1.9 and then use those entries to find the mean in the second column you can do this:
f = find(a(:,1)>=1 & a(:,1)<=1.9)
m = mean(a(f,2))
find will find the values that lie within this range and return the index, and a(f,2) accesses those indices in the in the second column and takes the mean. You can also do it with one line like so:
m = mean(a((a(:,1)>=1 & a(:,1)<=1.9),2))

How do I find frequency of values appearing in all rows of a matrix in MATLAB?

I have a 161*32 matrix (labelled "indpic") in MATLAB and I'm trying to find the frequency of a given number appearing in a row. So I think that I need to analyse each row separately for each value, but I'm incredibly unsure about how to go about this (I'm only new to MATLAB). This also means I'm incredibly useless with loops and whatnot as well.
Any help would be greatly appreciated!
If you want to count the number of times a specific number appears in each row, you can do this:
sum(indpic == val, 2)
where indpic is your matrix (e.g image) and val is the desired value to be counted.
Explanation: checking equality of each element with the value produces a boolean matrix with "1"s at the locations of the counted value. Summing each row (i.e summing along the 2nd dimension results in the desired column vector, where each element being equal to the number of times val is repeated in the corresponding row).
If you want to count how many times each value is repeated in your image, this is called a histogram, and you can use the histc command to achieve that. For example:
histc(indpic, 1:256)
counts how many times each value from 1 to 256 appears in image indpic.
Like this,
sum(indpic(rownum,:) == 7)
obviously change 7 to whatever.
You can just write
length(find(indpic(row_num,:)==some_value))
and it will give you the number of elements equal to "some_value" in the "row_num"th row in matrix "indpic"