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.
Related
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
I have a huge matrix in MATLAB. Now some rows contain only zeros.
Can I use the function find to find all the rows which contain only zeros ?
You can use any to find any rows that have non-zeros and then negate the result. We use the second input to specify that we want to apply the operation across the columns (the 2nd dimension).
row_has_all_zeros = ~any(data, 2)
If you want the indices instead of the logical array, just apply find to the result:
indices = find(row_has_all_zeros);
If you need, you can obtain more speed (depending on your dataset) by first looking for partial zero rows (in this example length 10) and computing further with the selection of rows.
row_has_first10_zeros = sum(data(:,1:10),2);
row_has_all_zeros = sum(data(~rows,:),2);
indices = find(~row_has_first10_zeros)
indices = indices(~row_has_all_zeros)
n = length(matrix);
This line will give you the number of rows in a matrix.
ids = setdiff(1:n,find(sum(matrix,2)));
ids will give you the row numbers(indexes) which contain only zeros.
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.
In Matlab I have a large matrix A. The first column of the matrix contains a time in seconds. The second to 13th column contain results from a calculation. For each column (except the first) I calculated the whisker by:
quantile(A,[.75])-1.5*(quantile(A,[.75])-quantile(A,[.25]))
Now I would like to now how many outliers (= values below whisker) there are in each column, and when they occur. This will give me the ability to calculate how much the outliers are spread over time.
I prefer to create a loop which gives me 12 martices containing two columns. The second column should contain the values of the outliers (= values of cells below whisker) without any zero's in between, and the first column should contain the time at which a outlier occurs (chronologically).
How can I create this?
regards,
Vincent
let,
A =
0.6260 0.7690 0.1209 0.5523 0.0495
0.6609 0.5814 0.8627 0.6299 0.4896
0.7298 0.9283 0.4843 0.0320 0.1925
0.8908 0.5801 0.8449 0.6147 0.1231
0.9823 0.0170 0.2094 0.3624 0.2055
for second column:
B = quantile(A(:,2),[.75])-1.5*(quantile(A(:,2),[.75])-quantile(A(:,2),[.25]))
Then,
index = find(A(:,2) < B)
value_outliner = A(index,2)
outliner_time = A(index,1)
No need to loop: use matrix operations and logical indexing instead.
Assuming you have a matrix A and outlier threshold thr is a 1x12 vector with the threshold for each column:
vals = A(:,2:13);
outliers = bsxfun(#lt, vals, thr); #% #lt is 'less than' function handle
#% outliers is a Nx12 logical matrix with true(1) where the value < threshold
#% and false(0) otherwise.
To get the time when these outliers occurred (for a given column, let's say column 2 of the data portion of the original matrix):
t = A(outliers(:,2), 1);
#% ^____________ logical index of rows where outliers occurred in that column
You can also easily get the number of outliers in each column (or row) by summing:
num_outliers = sum(outliers,1);
I hope to gather last lines from each submatrix or cell arrays.
I have 17 x 20 cells in matrix name A.
Each submatrices have different number of lines, but same number of columns (total 7 columns, all)
I tried to generate a file, made up of only last rows of each submatrices. My target file's from will be
M = [column1 column2 column3 column4 column5 column6 column7]
% made up of last rows of each submatrices, unknown number of lines, 7 columns
So I tried
for x_cc = 1:20
for y_cc = 1:17
M = A{x_cc, y_cc}(end,:);
end
end
But it is not working, giving the error Subscript indices must either be real positive integers or logicals.
Should I need to define the size first? What operation should be done? or what commands are useful? I tried cellfun, but not sure how can I use here.
Need any help to solve this situation. Thanks~!
First off, it looks like you switched x_cc and y_cc. Since your matrix is 17 x 20, x_cc is the rows and should go to 17, while y_cc will go to 20.
However, the error you're getting is probably coming from trying to index an empty array (one of those contained in A) using end. An example of this error:
a = [];
a(end)
??? Subscript indices must either be real positive integers or logicals.
If you're curious, a method avoiding for loops would look like:
B = cellfun(#(x) x(end,:), A, 'UniformOutput', 0);
M = cell2mat(B(:));
This grabs the last row from each matrix in A, then stacks them vertically and transforms to an array.