to use ismember with arraycell

gg =
4×1 cell array
{'A' }
{'A'}
{'B' }
{'A' }
[a,b]=ismember({'A'},gg)
a =
logical
1
b =
1
is not corret..i expect 1 1 0 1

1 comentario

"is not corret.."
It is correct: the first input 'A' is definitely a member of the second input:
ismember('A',{'A','A','B','A'})
ans = logical
1
"i expect 1 1 0 1"
ismember({'A','A','B','A'},'A')
ans = 1x4 logical array
1 1 0 1

Iniciar sesión para comentar.

 Respuesta aceptada

Sameer
Sameer el 13 de Oct. de 2024
Editada: Sameer el 13 de Oct. de 2024
Hi Luca
The "ismember" function is designed to work with arrays, and when using it with cell arrays of strings, it checks for membership of each element in the cell array. However, it returns a single logical value for the entire array when you provide a single element to check against the cell array.
To achieve the result you expect , you can use "strcmp" to compare each element in the cell array individually.
Here's how you can do it:
gg = {'A'; 'A'; 'B'; 'A'};
a = strcmp('A', gg);
disp(a);
1 1 0 1
Please refer to the below MathWorks documentation link:
Hope this helps!

1 comentario

[a,b]=ismember({'A'},gg)
answers the question of whether 'A' occurs in gg, as a yes or no answer in a and the index inside gg it occurs in b . {'A'} is a single element so you get a single answer.
[a,b]=ismember(gg, {'A'})
on the other hand iterates through the elements of gg, and for each one answers the question of whether the element occurs within {'A'} . gg is length 4 so the answer in a will be of length 4 -- [gg{1} is in {'A'}, gg{2} is in {'A'}, gg{3} is in {'A'}, gg{4} is in {'A'}] .

Iniciar sesión para comentar.

Más respuestas (2)

Mr. Pavl M.
Mr. Pavl M. el 13 de Oct. de 2024
gg = {'A' ,'A','B' ,'A' }
gg = 1x4 cell array
{'A'} {'A'} {'B'} {'A'}
a = cell2mat(gg)
a = 'AABA'
g = zeros(1,length(a));
g(find(a=='A')) = 1
g = 1×4
1 1 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

7 comentarios

shamal
shamal el 13 de Oct. de 2024
hi, i think it's not a perfect solution
Matt J
Matt J el 13 de Oct. de 2024
Editada: Matt J el 13 de Oct. de 2024
hi, i think it's not a perfect solution
@Luca Re but you don't tell us why the solution is inadequate. How would you expect the conversation to continue???
Mr. Pavl M.
Mr. Pavl M. el 13 de Oct. de 2024
Editada: Mr. Pavl M. el 13 de Oct. de 2024
What?
My solution is confidently correct, adequate, working good, performing sane, functioning well for numerous reasons, 1st it doesn't require hardcoding of the symbols, as you mistakenly accepted not my and not of Matt J. and not the last shortest one, another before.
There are N questions : to N answers to match, to fit finally. As any d.l. like natural system I can do of course more optimal & better if a customer is going to reward and remunerates me well.
Of course the best(shortest one) is this:
[a2, b2] = ismember(gg, {'A'})
If you are sufficiently attentive to the great details as well as I you will see, that shortest (in terms of system clock steps and memory footprint) is the solution followed after my in time and also they used ';' intracell delimeter, while in my solution the ',' intracell delimiter is more close to original and simpler.
Here is my shortened version:
gg = {'A' ,'A','B' ,'A' }
gg = 1x4 cell array
{'A'} {'A'} {'B'} {'A'}
g= zeros(1,length(cell2mat(gg)))
g = 1×4
0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
g(find( cell2mat(gg) == 'A') ) = 1
g = 1×4
1 1 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Please let me know which way to help optimally as per custom requirements, what to keep doing.
shamal
shamal el 13 de Oct. de 2024
Editada: shamal el 13 de Oct. de 2024
You are adding characters
The one above was a simplified example but if my data were a union of characters:
gg = { 'STA' , 'STAT' , 'STA' , 'STATT' }
gg = 1x4 cell array
{'STA'} {'STAT'} {'STAA'} {'STATT'}
g= zeros(1,length(cell2mat(gg)))
g = 1×16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
g(find( cell2mat(gg) == 'A') ) = 1
g = 1×16
0 0 1 0 0 1 0 0 0 1 1 0 0 1 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
it's not correct
Codes should be designed generically because I can't know the inputs I give them
sorry if I'm making a bit of a mess with the examples.. actually I want:
dd = { 'STA', 'STAT', 'STA', 'STATT' } ;
a = cell2mat(dd);
g = zeros(1,length(a));
g(find(a=='STA')) = 1
Arrays have incompatible sizes for this operation.
Mr. Pavl M.
Mr. Pavl M. el 14 de Oct. de 2024
Editada: Mr. Pavl M. el 14 de Oct. de 2024
clc
clear all
close all
%% Inputs ( from a methode ):
ex = 'STA';
dd = { 'STA', 'STAT', 'STA', 'STATT' } ;
%% Detection module:
% contains(dd, 'STA') is not yet implemented as built-in in TCE Octave
if length(ex)>1
strcmp(dd,ex)
else
a = cell2mat(dd);
g = zeros(1,length(a));
g(find(a==ex)) = 1
end
ans = 1x4 logical array
1 0 1 0
shamal
shamal el 14 de Oct. de 2024
Editada: shamal el 14 de Oct. de 2024
ok but i think this solution is better: (solution of Sameer)
gg = {'AB'; 'AA'; 'BB'; 'AC'};
a = strcmp('AC', gg)
It has fewer lines of code..it seems faster to me

Iniciar sesión para comentar.

Steven Lord
Steven Lord el 13 de Oct. de 2024
Both outputs are the same size as the first input.
gg = {'A'; 'A'; 'B'; 'A'};
[a1,b1]=ismember({'A'},gg)
a1 = logical
1
b1 = 1
[a2, b2] = ismember(gg, {'A'})
a2 = 4x1 logical array
1 1 0 1
b2 = 4×1
1 1 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

2 comentarios

shamal
shamal el 13 de Oct. de 2024
correct!
Based on a revised example you posted as a comment on another answer, it looks like you're not trying to find cells in a cell array that are an exact match for a specific character. It looks like you're trying to find cells that contain that character anywhere inside the word they contain. If that is correct:
gg = { 'STA' , 'STAT' , 'STA' , 'STATT' }
gg = 1x4 cell array
{'STA'} {'STAT'} {'STA'} {'STATT'}
contains(gg, 'A')
ans = 1x4 logical array
1 1 1 1
gg{3} = 'STEM' % Replace one of the words with one not containing 'A'
gg = 1x4 cell array
{'STA'} {'STAT'} {'STEM'} {'STATT'}
contains(gg, 'A')
ans = 1x4 logical array
1 1 0 1
Or you could use other string related functions instead of contains. Some examples include startsWith and endsWith.

Iniciar sesión para comentar.

Preguntada:

el 13 de Oct. de 2024

Editada:

el 14 de Oct. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by