1

Say that we have a 3x3 matrix in matlab. If we type x(:), this will select all the elements in the matrix, right?

How can we select all the elecments except element x(2,2)? What should we type in this case?

Thanks.

Amro
  • 123,847
  • 25
  • 243
  • 454
Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • Possible duplicate? http://stackoverflow.com/q/11054728/2065121 – Roger Rowland May 23 '13 at 09:47
  • 1
    @EitanT: I just created a new tag [tag:matrix-indexing], which should apply to lots of questions.. I cant believe this hasn't been done before :) The existing tags refer to databases indexing – Amro May 23 '13 at 12:47
  • thanks, now lets hope others pick up on it. – Amro May 23 '13 at 12:55
  • @Amro I've done my share of tagging now (by the way, I've also noticed the misuse of the [tag:delete] tag, which really refers to deleting files, not elements from an array). – Eitan T May 23 '13 at 13:20
  • 1
    @EitanT: appreciate it, thanks. I wish stack exchange would introduce new 10K tools for mass retagging :) – Amro May 23 '13 at 13:27
  • @Amro Nice idea, you should post it on Meta. Some tool that accepts rules, for example: "_retag all [tag:index]-tagged questions that are also [tag:matlab] questions with [tag:matlab-indexing]_". – Eitan T May 23 '13 at 14:01
  • I wonder if moderators have access to such tools... Another area lacking is synonyms suggestion; speaking of which could you take a look at this one: http://stackoverflow.com/tags/matlab-compiler/synonyms , its so hard to get attention to these suggestions – Amro May 23 '13 at 14:18

2 Answers2

1

I would do it like this: first, create a logical array of trues, which would select all elements if used as an index mask:

mask = true(size(x) );

Now set element 2,2 to be false, therefore deselecting it:

mask(2,2) = false;

Now use this mask to select elements from x:

myValues = x(mask);

EDIT: Removed second, incorrect answer.

devrobf
  • 6,973
  • 2
  • 32
  • 46
  • Thanks for your reply. When I tried your *simpler* solution, I got this error: `Subscripted assignment dimension mismatch.`. Why is that? – Simplicity May 23 '13 at 09:58
  • You can't remove an element that would result in a "non-rectangular matrix", so to speak. The method based on logical indexing is more expressive, anyways, and does not involve mutating a matrix. – Joshua Barr May 23 '13 at 10:04
  • @JoshuaBarr You're correct, apologies. As I said I saw it on another answer, I hadn't followed it through properly. – devrobf May 23 '13 at 10:55
  • @Med-SWEng My mistake (see Joshua's comment), go with the first solution in this case! – devrobf May 23 '13 at 10:56
  • @jazzbassrob The second answer is also fine, with a minor adjustments. Convert the original matrix into a column vector first and then use or linear indices (or `sub2ind`) to remove elements. – Eitan T May 23 '13 at 12:01
0

You could use:

 A(setdiff(1:numel(A),ceil(numel(A)/2)))

For example, for the input as:

>> A = randi(100,3)

A =

    49    71    68
    45    76    66
    65    28    17

The output is:

>> A(setdiff(1:numel(A),ceil(numel(A)/2)))

ans =

    49    45    65    71    28    68    66    17
Roney Michael
  • 3,964
  • 5
  • 30
  • 45