[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Cell array membership (was: Iterating over cell arrays)
From: |
Hamish Allan |
Subject: |
Cell array membership (was: Iterating over cell arrays) |
Date: |
Tue, 25 Jan 2005 16:11:30 +0000 |
Since I posted this, a friend of mine has told me that Matlab 7 works
in the way I would expect. Is it therefore reasonable to make this a
feature request for Octave?
In the meantime, here is the hack I used in case anyone else wants to
do this (please post better solutions though!):
%%%%
function t = iscellarraymember(item, arr)
for i=1:length(arr)
t = equal(item, arr{i});
if t
break;
end
end
end
function t = equal(m1, m2)
t = 0;
try
if (size(m1) == size(m2));
t = (m1 == m2);
end
catch
end
end
%%%%
Cheers,
Hamish
On Jan 25, 2005, at 2:06, Hamish Allan wrote:
Hi,
I'd like to be able to check for membership in a cell array, e.g.:
octave> cell_array = { 'Foo' 'Bar' 'Baz' 'FooBar' };
octave> ismember('Bar', cell_array)
ans =
1 1 1
octave> ismember('FooBar', cell_array)
ans =
1 1 1 1 1 1
octave> ismember('z', cell_array) % NB if cell_array were a character
matrix this would return 'ans = 1'
ans = 0
But of course I get:
error: binary operator `==' not implemented for `cell' by `cell'
operations
error: evaluating binary operator `==' near line 67, column 24
[etc.]
Because there is no guarantee that cell matrices will be comparable,
none are compared.
Two queries:
1) Would it be reasonable for ismember() to be designed to behave in
the way I describe above?
2) Is there a simpler way to do what I want to do than writing a
function to iterate over the members of cell_array? I don't even need
the intersection, I just want a true or false test for membership.
Many thanks,
Hamish
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------