0

Let's assume:

R = [1     2     3; ...
     4     5     6; ...
     7     8     9; ...
     ];

How I can pick* only three members from it: R(1,1) , R(1,3) and R(3,3)

It means I am looking for

 B =
     1
     7 
     9 

Please bear in mind that B = [R(1,1), R(1,3), R(3,3)] or "loop" solution is not the answer as I need to do it for a large data. So, preferably using indexing solution. Thanks

  • The rule (condition) for picking the member is: R(i,j) that i = [ 1, 3, 3] and j = [1 , 1 , 3];
Iman
  • 412
  • 4
  • 18
  • 2
    `B = [R(1,1), R(1,3), R(3,3)]` – sco1 Jun 17 '16 at 17:16
  • Thanks for your response. But, I need to do this procedure for a huge data. So, I will edit the question to emphasize that I am not looking for that manual recalling. – Iman Jun 17 '16 at 17:18
  • 1
    @Iman be sure to mention the rule by which you want to pick your numbers... Are these some numbers at random? Or just the non-top-right corners? – Dev-iL Jun 17 '16 at 17:21
  • OK, I will edit the question to mention the rule. – Iman Jun 17 '16 at 17:22
  • @excaza Let me check the question. Thanks – Iman Jun 17 '16 at 17:30
  • @excaza Thanks for your help. So, I don't know should I mention the answer here or just remove my question. Any thoughts? Using linear indexing directly without calling SUB2IND is the fastest solution: rows = [1 3 3]; columns = [1 1 3]; output = R( size(R,1)*(columns -1) + rows ) – Iman Jun 17 '16 at 17:37
  • 1
    @excaza OK. I will remove it. Thanks though for your response and time <3 – Iman Jun 17 '16 at 17:46
  • @excaza I am about to delete my question. But, the site says deleting my question will give me some negative points. Is that right? – Iman Jun 17 '16 at 17:49
  • @Iman you will lose the 2 points you gained from accepting the answer. – sco1 Jun 17 '16 at 17:58
  • Noooo no no! Don't delete questions with upvoted answers! That's bad! While the question might be redundant, we have to appreciate the work made by others in answering your post. I know it's not your fault @Iman and thanks for being compliant:) But answered questions are not safe to delete. – Andras Deak -- Слава Україні Jun 17 '16 at 18:00
  • 1
    @AndrasDeak Ok, I got confused with whethe I delete it or not! So, I will leave it like that for a while. I have to go now. But, rethink over it later. Cheers – Iman Jun 17 '16 at 18:02
  • @Iman just leave it around. Duplicates are not necessarily a bad thing, only bad duplicates are. [Here's a meta post](http://meta.stackoverflow.com/a/311812/5067311) which notes that self-deleting after an answer is very bad, and considered a hostile gesture. Even if everybody involved (mainly Suever and you) agrees, it would count against you a lot. If there are no answers yet, then self-deleting is OK:) – Andras Deak -- Слава Україні Jun 17 '16 at 18:05

2 Answers2

2

You can simply use ismember to find all 1, 7, or 9's

B = A(ismember(A, [1 7 9]));

otherwise if you want specific locations and you have the rows and columns, just use sub2ind to create a linear index

rows = [1 3 3];
columns = [1 1 3];

B = A(sub2ind(size(A), rows, columns));

As the dupe answer points out you can definitely do the following but you sacrifice readability.

B = A(size(A, 1) * (columns - 1) + rows);
Suever
  • 64,497
  • 14
  • 82
  • 101
  • 1
    Thanks for your replying. But, firstly ismember is relatively slow for large data. Because, it will search all of members for find the result. Secondly, I am looking for a solution to select members with respect to their index. – Iman Jun 17 '16 at 17:25
  • @Iman Please read the entire answer... – Suever Jun 17 '16 at 17:26
  • @Iman Also if what you actually need to do was the first thing, then `ismember` is going to be as fast as you can get. It will certainly be faster than using `find` or any similar function – Suever Jun 17 '16 at 17:28
  • Oh, I just see the second part. Let me run it in my code. I'll pick it as a correct answer if it works quickly. Thanks – Iman Jun 17 '16 at 17:28
  • Thank for your time. I found a solution that is probably the fastest method: rows = [1 3 3]; columns = [1 1 3]; output = R( size(R,1)*(columns -1) + rows ) – Iman Jun 17 '16 at 17:38
0
R = [1 2 3; 4 5 6; 7 8 9]; % Your matrix
B = R([1,3,9])             % indices
OsJoe
  • 259
  • 1
  • 4
  • 9
  • thank you for your answer. Your answer is working perfectly accurate and fast. But, I was looking for a solution with indexing. Please read my above comment. – Iman Jun 17 '16 at 17:40
  • I'm not sure what's going on here. The `R(:)` is completely unnecessary and the OP doesn't know the linear indices. – Suever Jun 17 '16 at 17:41
  • `original col*original row` is not how you get linear indices from subscripts. – sco1 Jun 17 '16 at 17:47
  • Thanks Suever! I have updated the answer. – OsJoe Jun 17 '16 at 17:47
  • @OsJoe He still doesn't know the values `[1 3 9]`... – Suever Jun 17 '16 at 17:50