-1

I have a matrix (about 342 by 342) denoted by C_{ij} and I want to identify all indices i,j which satisfy the condition C_{ij} > rho where rho is some fixed value. I am using MATLAB

For example, if I have the matrix C_{ij} as:

C = 1     0.7    0.8
    0.7   1      0.5
    0.8   0.5    1

And rho = 0.6 then the indices which satisfy the condition C_{ij} > 0.6 are i,j = 1,2 as C_{11}=C_{22}=1 and C_{12}=C_{21}=0.7 But note that i,j=3 does not satisfy this condition since although C_{13}=C_{31}=0.8, C_{23}=C_{32}=0.5

I am not sure how/the best way to do this is in MATLAB?

Adriaan
  • 15,941
  • 7
  • 35
  • 67
Ansh
  • 69
  • 6
  • `[k,l]=find(C>rho);` – Andras Deak Jan 19 '16 at 15:06
  • Your question is unclear. What's the criterion to include or not include a given pair `i,j` in the result? Is `C` always symmetric? – Luis Mendo Jan 19 '16 at 15:13
  • @LuisMendo I read the criterion as "needs to be larger than rho", and whether `C` is symmetric or not isn't really relevant, besides that if it is symmetric you might use `[rowt,colt,~] = find(triu(C)>rho]; row = [rowt;colt]; col = [colt;rowt]`. I'm not sure whether that's faster than simply using a logical on the full matrix. – Adriaan Jan 19 '16 at 15:18
  • @Adriaan You're probnably right, as you have 4 upvotes by now :-) But I don't see how `C(2,3)` influences the decision for `i=3`, `j=3` according to the OP – Luis Mendo Jan 19 '16 at 15:23
  • Upvotes are by no means a way of telling whether someone's correct or not [e.g. here](http://stackoverflow.com/questions/34725121/which-toolbox-can-perform-mimo-system-identification-in-matlab/34725330#34725330) ;). I think the OP's unsure about the row/column structure and whether operators work along those. – Adriaan Jan 19 '16 at 15:25
  • 1
    @Adriaan Hm... you were jealous of Ray's three-letter answer, weren't you? :-) – Luis Mendo Jan 19 '16 at 15:26
  • 3
    Are the top-left/bottom-right corners of the square submatrix always on the diagonal, or could you have a submatrix bounded by `C(2,1),C(3,2)`? – beaker Jan 19 '16 at 18:35

1 Answers1

7
C = rand(342,342);
rho = 0.6;
res = C(C>rho); %// contains all values that are above the threshold
[row,col,val] = find(res); %// returns the indices.

row would contain your i, col your j, but I'd caution against using i or j as a variable. val contains the corresponding value, but you can omit that for freeing RAM, as you can use C(row,col) as well to get the values.

If your matrix is symmetric you can use that:

[rowt,colt,val] = find(triu(C)>rho); %// find only in the upper triangle
row = [rowt;colt]; %// flip rows and columns to obtain all results
col = [colt;rowt]; %// flip rows and columns to obtain all results

For your edit:

IdxR = find(diff(unique(rows))==1,1,'first');
IdxC = find(diff(unique(columns))==1,1,'first');
Result = C(1:IdxR,1:IdxC);
Community
  • 1
  • 1
Adriaan
  • 15,941
  • 7
  • 35
  • 67
  • "`and I want to identify all indices i,j which satisfy the condition`" – Andras Deak Jan 19 '16 at 15:07
  • 1
    @AndrasDeak "retaining and identifying" according to his title, now it does both – Adriaan Jan 19 '16 at 15:15
  • @Adriaan @AndrasDeak Yes the matrix is always symmetric. although I would prefer a method which doesn't assume this. Perhaps I didn't explain my criterion correctly. Using the matrix in my original question as an example, that first code would identify `k` and `l` to be 1, 2 and 3. However, that would include the element `C(2,3)` and `C(3,2)` which are both < rho. Hopefully that clear things up a little. It's rather hard to explain this without LaTeX – Ansh Jan 19 '16 at 16:44
  • @Ansh this code would not include those elements, as they are not above the threshold. – Adriaan Jan 19 '16 at 16:47
  • @Ansh have you tried the above (general) solution on a specific matrix? What makes you think that it will return indices that correspond to values which violate the comparison? – Andras Deak Jan 19 '16 at 17:12
  • @Adriaan ah I see the confusion now (partly because I've confused myself). I should specify that since C(2,3) and indeed C(3,2) do not satisfy the condition I want only the following block to be retained from the original matrix: [1 , 0.7 ; 0.7 , 1] - i.e. for this case the C(1,1), C(2,2), C(1,2) and C(2,1) elements i.e. in this particular case the upper left 2 by 2 block. – Ansh Jan 19 '16 at 17:14
  • @AndrasDeak Yes I did indeed try that solution for the matrix given in my question. Hopefully my comment above clarifies things better. – Ansh Jan 19 '16 at 17:20
  • 1
    @Ansh Adriaan's answer *doesn't* give you `C(3,2)`, but `C(3,1)` it does. Is *that* your problem? Then your not after individual elements, but full rectangular blocks which abide by the condition. Is that what you want? Submatrices given by `C(indexvector1,indexvector2)`? If yes, you need to really rephrase your question, as it's a much more difficult problem, and needs a different solution. – Andras Deak Jan 19 '16 at 17:27
  • 1
    @Ansh Clarifications should be edited into your question. If your question is not self-contained and clear, it should be closed. – beaker Jan 19 '16 at 17:41
  • @AndrasDeak - the question has now been edited to make it clearer as to what it is I exactly want. Does this help? – Ansh Jan 19 '16 at 17:50
  • @Ansh almost: square or rectangular submatrix? Can the result be non-square? – Andras Deak Jan 19 '16 at 18:08
  • @AndrasDeak No the submatrix must be square given the way I want the threshold condition to work – Ansh Jan 20 '16 at 11:25