How to compare two string are equal or not
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mekala balaji
le 28 Sep 2018
Réponse apportée : Image Analyst
le 28 Sep 2018
Hi,
I have two string (names array in cell matrix), and want to compare ith & i-1th name is equal or not.
Names={'Mhajj56_Too';'YHJA_90_Kty';'Mahjkl_uiT00';'Mahjkl_uiT00';'aha';'Mhajj56_Too';'Mhajj56_Too'}
out=[different
different
same
different
different
same]
Rule: if ith name is same as i-1th, then we say same, else different.
Many thanks in advance,
0 commentaires
Réponse acceptée
Image Analyst
le 28 Sep 2018
A one-liner way is:
outLogical = cellfun(@isequal, Names(1:end-1), Names(2:end))
(Note that what you put is not valid syntax unless same and different are variables, not strings.)
0 commentaires
Plus de réponses (1)
Image Analyst
le 28 Sep 2018
Well the obvious, simplest way is with isequal() in a for loop:
Names={'Mhajj56_Too';'YHJA_90_Kty';'Mahjkl_uiT00';'Mahjkl_uiT00';'aha';'Mhajj56_Too';'Mhajj56_Too'}'
numberOfCells = length(Names)
outLogical = false(1, numberOfCells-1);
for k = 2 : numberOfCells
if isequal(Names{k}, Names{k-1})
outCellArray{k-1} = 'Same';
outLogical(k-1) = true;
else
outCellArray{k-1} = 'Different';
end
end
% Show in command window:
celldisp(outCellArray);
outLogical
0 commentaires
Voir également
Catégories
En savoir plus sur Database Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!