2

Wondering if anyone could help me with this.

For getting the first column x of a matrix A. I use x = A(:,1). Every so often, the matrix A is empty, in which case I would like my column to also be empty. But with Matlab, the code exits with error “Index exceeds matrix dimensions”. Is there a way to prevent it from exiting, and instead give me []?

(I could of course write an “if” statement using isempty(A), but that’s annoying since my code is filled with dozens of places where I may have empty matrices.)

Amro
  • 123,847
  • 25
  • 243
  • 454
  • perhaps you can use cell arrays, that way you could have: `A{1} = []` or `A{1} = rand(10,1)`, and always access it as: `x = A{1}` – Amro Sep 06 '13 at 23:35

2 Answers2

3

This is kinda clumsy, but it works and is shorter than an if or try:

A(:,1:min(1,size(A,2)))

or

A(:,1:9999999999:size(A,2))
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 2
    why the downvote? It is perfectly valid, and a clever trick. The first one can be made friendlier: `A(:,1:min(1,end))`. Same for the second one: `A(:,1:end+1:end)`. Still I wouldnt use it in my code :) – Amro Sep 06 '13 at 23:32
  • @Amro wow, I never knew you could use `end` as a function argument like that. – Bas Swinckels Sep 07 '13 at 06:28
  • @BasSwinckels: this comes from the fact that you can also overload the [`end`](http://www.mathworks.com/help/matlab/ref/end.html) method for user-defined classes and customize the indexing behavior for objects: http://www.mathworks.com/help/matlab/matlab_oop/indexed-reference-and-assignment.html#br1dtdk-1 – Amro Sep 07 '13 at 14:06
  • @Amro: Actually, I think it's just because `end` is being used inside the second subscript, so MATLAB knows you're talking about `size(A,2)`. `min` isn't an array, so `end` can't mean the size of `min`. – Ben Voigt Sep 07 '13 at 23:19
  • @BenVoigt: I understand that, I was just providing what I thought were relevant documentation links :) If you want more bizarre behavior, try: `A(:,min(1,end))` vs. `f = @min; A(:,f(1,end))`. The parser interprets `end` statement incorrectly here, thinking that `f` is an array to be indexed, even though it is a function handle.. If you wanna see what's going on, create a file named `end.m` in the current directory overriding the builtin function: `function ind = end(o,k,n), keyboard, ind = builtin('end',o,k,n); end` – Amro Sep 08 '13 at 00:46
3

You can use logical indexing:

A(:,end>0);

For non-empty matrices it will be

A(:,logical(1));

which returns the first column, and for empty matrices it will be

A(:,logical(0));

which returns an empty column matrix.

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
  • Doesn't logical indexing have to be the same size as the array dimension being indexed? It seems to work though – Ben Voigt Sep 07 '13 at 19:29
  • 1
    @BenVoigt No it doesn't, only the columns that are being selected should exist. So for a 2-by-2 matrix it is ok to use `A(:, logical([0 1 0])` but not `A(:, logical([0 0 1])`. – Mohsen Nosratinia Sep 07 '13 at 22:37