I have a data like below:
49.6 46.1
49.65 46.3
50.1 47.03
50.2 47.06
51.35 46.027
51.36 46.20
I want to find the mean values of both column based on specific range of first column. for example in range of [49-50) I should calculate the mean values of the first column and mean of corresponding values in the second column. In this example the sub-array (first column only) with numbers
49.6
49.65
will be in range of [49-50) so I want to find the mean value for them and the mean value of the corresponding values in the 2nd column.
The range would be like 49:1:100. The code below doesn't work properly.
for i=49:1:100
meanWithinRange(i) = mean(data(i,1));
end
I think you are looking for logical indexing.
First, create a logical array for the in-range values of column 1:
A=[49.6 46.1
49.65 46.3
50.1 47.03
50.2 47.06
51.35 46.027
51.36 46.20];
I = A(:,1)>=49 & A(:,1)<50;
I is a logical column vector, and is true for the rows that are in range. You can use this to index the rows you want:
>> A(I,:)
ans =
49.6000 46.1000
49.6500 46.3000
So now you can simply compute the mean of this result:
>> mean(A(I,:))
ans =
49.6250 46.2000
Related
How can I get the numeric values in a column (let's say column 10) when the numeric values in another column (let's say column 9) are equal to a specific number and plot this in a graph.
e.g., When values of column 9 == 4, get the corresponding value of column 10 and plot. I am using row index number as a marker for time.
I am plotting all of column 10 to get a waveform then I want to use the data of column 9 to add markers to my waveform that are representative of a command occurring at a certain point in time.
Here is my code:
E = csvread('Experiment_at_10_45_1.csv');
[signal_rows, signal_columns] = size(E);
t=(1:signal_rows)/128; %128 samples per second
%% SNR plot for down frequency
plot(t,E(:,13),'k')
I hope my explanation is clear, as I have attempted to use a minimum working example of my code for the first time.
You'll want to use logical indexing to do this. You want to first create an array of 0 (false) and 1 (true) values where column 9 is equal to the value you want.
bool = E(:,9) == 4;
Then you'll want to use this 0 and 1 array as the row index. This will grab only the rows where column 9 was equal to 4. This is referred to as logical indexing.
E(bool, 10)
Then you can plot this
plot(t(bool), E(bool, 10))
As pointed out though, it is possible that the values aren't exactly to 4 due to floating point representation. To get around this, you just want to check if they are "close enough" using a very small epsilon.
bool = abs(E(:,9) - 4) < 1e-12;
I have created a function that should be able to read in any mxn matrix and gives me the maximum value of the entire matrix (not just per column) and what its indices are.
function [ BIGGEST ] = singlemax( x )
[Largest_values row]=max(x)
[biggest_number column] = max(max(x))
end
This function gives me all the information I need, however it is not very clean as it gets messy the larger the matrix.
The real problem area is printing out the row in which the maxima is located.
Largest_values =
0.7750 0.9122 0.7672 0.9500 0.6871
row =
3 2 3 2 2
biggest_number =
0.9500
column =
4
This is my print out given a random matrix as an input.With the function I have created I cannot read the indices of my max value in any given array using a created function. If I could somehow relate the maximas from each column and there corresponding row (such as making the results a matrix with the column max on top and the row index on bottom, all within the same respective columns )I could display the row of the absolute maximum.
Here's one approach:
value = max(x(:));
[rowIndex,columnIndex] = ind2sub(size(x),find(x==value));
Read the ind2sub documentation for more details.
Edited to modify so that it finds indices of all occurrences of the maximum value.
I am a newcomer struggling with writing a code in MatLab which is extremely useful for my research. I have csv files with many columns, but interested in only two of them, say columns X and Y, all numbers. What I need to do is picking certain number from X column having approximate corresponding number in Y column. For example, picking a number in X column that has closest-to-54 corresponding Y. Can I do it with MatLab?
I appreciate any help.
Suppose you have your matrix data like this:
data =
0.6293 -2.5788 -0.9027 53.0100 -0.7203
0.3602 2.0267 0.2537 54.5300 -0.0149
0.0870 -0.7028 1.2209 54.6034 0.5088
-1.1552 1.5505 0.3589 54.0044 1.1467
1.7230 -0.5437 0.1975 56.3978 -0.5802
1.9416 0.7137 0.2764 55.0810 -0.2298
0.4837 -0.1987 0.8526 53.6421 -0.4461
-0.1257 1.7165 -0.8384 53.7798 1.3852
0.6433 0.6902 -1.1979 53.9323 -1.2776
1.6593 -0.5343 -1.3725 52.8364 -0.0738
And you want to find the value on column X = 2 where the value on the same row, on column Y = 4, is closest to 54. You could do it like this:
X = 2; % Index of the column to retrieve the value from
Y = 4; % Index of the column to find the value nearest to 54
[~,idx] = min(abs(data(:,Y) - 54))
data(idx,X)
Which would give you this:
ans =
1.5505
What it's doing is taking the absolute difference between column Y and 54 and retrieving the index of the row of the element with minimum difference. Then, this index is used to retrieve the value of that line, on column X.
I have a Matrix A of 1000 rows and 2 columns.
A = [0.0325 5.6 ; 0.0367 7.6 ; 0.0391 8.1 ; 0.0404 9.7; etc.]
I want to find the matching element in the 2nd column for a given value that is not necessarily in the first column.
For instance, for a given value 0.0371, I would want a value of 7.6 since it corresponds to the 2nd column value of the element that is the closest from my input 0.0371 (0.0367).
For 0.0393, I want 8.1, etc.
You can compute the difference between each of the elements and your desired value, then find the index of the minimum. The value you want will be at the index in column 2.
[~,idx] = min(abs(A(:,1)-testval));
desiredval = A(idx,2);
I'm doing data analysis in Matlab, and I have two columns. I'm using find(column1>0) to find the positive values of the first column in a data set. Now, I want to plot (column1,column2), but it is of course not possible, as the size is not the same. The question:
How do I get the corresponding values in column2 for the positive values in column1? Like, if row 17 and row 42 have a positive value in column1, how do I find the value of row 17 and row 42 in column2?
The term for what you are doing is indexing. You can use find, which generates linear indices, but you shouldn't in this case. Logical indexing is more appropriate.
index = column1 > 0; #% creates a logical index with true where the
#% condition is satisfied and false otherwise.
values1 = column1(index);
values2 = column2(index);
#% values1 and values2 will be the same size, since they were indexed the same
plot(values1,values2); #% or however you want to do it.