Relatively complex matrix indexing question
Mostra commenti meno recenti
Hi,
I have a reasonably complex matrix indexing question that I'm struggling to solve. I have an nxn matrix of float values. The matrix is fairly large (n~30000). The matrix is upper (or lower) triangular with the other values being NaN. Each of the rows represented has a vector associated with it, so the matrix rows has a secondary matrix associated with it that is nxk, where each row has k characteristics. Row 1 has the same characteristics as column 1, row 2 has the same characteristics as column2, etc.
I want to plot a histogram of all of the elements in the nxn matrix whose rows/columns match certain characteristics.
To illustrate, let's say I had the following 4x4 value matrix
Values = [...
NaN 4.0 3.1 2.3
NaN NaN 1.2 5.2
NaN NaN NaN 7.1
NaN NaN NaN NaN]
Each row/column has three characteristics and this characteristics matrix has the values
Charac = [...
4 2 3
1 4 5
-3 5 1
10 6 2]
I only want to get histogram elements for rows/columns that have the second characteristic greater than 1 and the third characteristic less than 4. In this case only the first, third and fourth rows would meet the criterion and the values that I would take from the matrix Values are 3.1, 2.3 and 7.1.
Is there a compact and computationally efficient way to logically index this?
Thanks in advance!
8 Commenti
Sara
il 9 Giu 2014
Why "the values that I would take from the matrix Values are 3.1, 2.3 and 7.1."?? How is the result related to "the first, third and fourth rows would meet the criterion"?
Sam
il 9 Giu 2014
Image Analyst
il 9 Giu 2014
4.0 at (1,2) is also in the first row. Why is it not to be selected?
Your logic for defining columns is still unclear to me. You get valid rows, and then you take all possible pairs of these row IDs which define rows/columns in the upper triangle? Your initial matrix takes ~7.2GB, which could indicate that there is an issue with the design of the computation. If not, you may want to profile both the dense and the sparse versions of this computation. How large is k by the way?
Sam
il 10 Giu 2014
Not sparse, not sparse, I'd say that it is half full at least ;-) I still don't get your columns selection; is it what I describe above? Seeing Image Analyst's solution, it seems that your statement is not clear to the two of us. Back to sparsity, uint8 is a great solution but many built-ins won't support it, so you'll see how far you go with that. If you are stuck with doubles, storing your large matrix as sparse would divide the size in memory by a little less than 2, and building it originally as sparse might be simpler than building is as dense (it happens sometimes, as you can build sparse matrices using vectors of row and column indices, as well as a vector of values).
Sam
il 10 Giu 2014
Cedric
il 10 Giu 2014
See my answer below.
Risposta accettata
Più risposte (1)
Image Analyst
il 9 Giu 2014
Try this and see if it's what you want:
Values = [...
NaN 4.0 3.1 2.3
NaN NaN 1.2 5.2
NaN NaN NaN 7.1
NaN NaN NaN NaN]
Charac = [...
4 2 3
1 4 5
-3 5 1
10 6 2]
% Find rows where column 2 > 1 and column 3 < 4
rowsToExtract = Charac(:, 2) > 1 & Charac(:, 3) < 4
% Extract only those rows and no others.
extractedRows = Values(rowsToExtract,:)
% Get the values from that which are not NaN.
extractedValues = extractedRows(~isnan(extractedRows(:)))
3 Commenti
Sam
il 10 Giu 2014
Image Analyst
il 10 Giu 2014
I don't see why my way would not work. Your suggestion will end up eliminating some columns also, and you didn't say anything about eliminating columns.
Sam
il 10 Giu 2014
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!