How to asses whether a single Char is member of an Array of Chars?

9 visualizzazioni (ultimi 30 giorni)
I try to code an experiment. I have a Matrix with 4 Rows, where 1 row contains numbers from 1:20 which should be matched with a char Array {'B'.....'Z'} without vowels. I have several Conditions in FOR Loop for every Row:
trial = 1;
for trial = 1:20
Trial(trial).Setsize = TrialMatrix(trial,1);
Trial(trial).UpdatingSteps = TrialMatrix(trial,2);
Trial(trial).Match = TrialMatrix(trial,3);
Trial(trial).ProbeStim = TrialMatrix(trial,4);
if Trial(trial).Match == 0
Stims = Stims(Stims ~= Trial(trial).ProbeStim);
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps);
clear Stims
elseif Trial(trial).Match == 1 && Trial(trial).UpdatingSteps == 0
cmpr0 = 0
while cmpr0 == 0
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps)
cmpr0 = ismember(Trial(trial).ProbeStim,Trial(trial).EncodingStims)
end
elseif Trial(trial).Match == 1 && Trial(trial).UpdatingSteps == 1
cmpr1 =0;
cmpr2 =1;
while cmpr1==0 && cmpr2==1
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps)
cmpr1 = ismember(Trial(trial).ProbeStim,Trial(trial).EncodingStims)
cmpr2 = strncmp(Trial(trial).ProbeStim,Trial(trial).EncodingStims,1)
end
elseif Trial(trial).Match == 1 && Trial(trial).UpdatingSteps == 2
cmpr2 =0;
cmpr3 =1;
while cmpr2 == 0 && cmpr3 == 1
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps)
cmpr2 = strcmp(Trial(trial).ProbeStim,Trial(trial).EncodingStims);
cmpr3 = strncmp(Trial(trial).ProbeStim,Trial(trial).EncodingStims,2)
end
end
end
Especially the while loops are not working properly, the Conditions are violated. I want to compare, whether a letter is
  1. a.) Part of a Set of Letters
  2. b.) On which position of this Set (should not be on the first and the second position in some cases).
I tried this with strncmp and ismember, but I think this functions don't return a single true or false for my "Probe" but a vector, so that the loops quit incorrectly.
The Condtions for "NoMatch" and "Match" with no Updating are working fine, if I don't "convert" the sampled stimuli to chars like
% bla =(1:20);
% Stims = expinfo.letters(bla);
% Trial(trial).ProbeStim = expinfo.letters(Trial(trial).ProbeStim)
My question: are there functions for single value comparisons of chars (didn't found some) and how can I handle this char / numbers problem? Maybe you habe ideas, I'm kinda desperate.
greetings
Jan

Risposta accettata

Stephen23
Stephen23 il 3 Set 2018
Modificato: Stephen23 il 3 Set 2018
"How to asses whether a single Char is member of an Array of Chars?"
"...single value comparisons of chars..."
Of course, just use ==:
>> 'C'=='ABCDE'
ans =
0 0 1 0 0
"with a char Array {'B'.....'Z'}"
Except what you shows us is not a char array, but is actually a cell array of char vectors (each of which appears to be a scalar char), and this is making your code more complex than it needs to be. You can trivially define a real char array like this:
'BCDFGH...Z'
and then the method I showed you will work. Using a cell array is likely a misdirection that complicates your code: if you only need an array of individual characters, then a simple char array/vector does the job perfectly.
You can easily use ismember to generate a character array of all non-vowel letters:
>> V = 'A':'Z'
V = ABCDEFGHIJKLMNOPQRSTUVWXYZ
>> V = V(~ismember(V,'AEIOU'))
V = BCDFGHJKLMNPQRSTVWXYZ
  3 Commenti
Stephen23
Stephen23 il 3 Set 2018
Modificato: Stephen23 il 3 Set 2018
@Jan Göttmann: probably you can use == or ismember. I have no idea what data you actually have, because your description and examples disagree with each other, and because pasting lots of code does not really help understand what data you have.
Now you write that you have "numbers", but you do not explain if this means char vectors that contain number representations, or something else... strings? Numeric arrays? Numeric scalars in a cell array? You don't say, and so I have no idea.
If you want help with this, please put both of the exact arrays/characters into one .mat file, and upload this by clicking the paperclip button. Do not give all of those structures, just the exact arrays that you want to compare.
Jan Göttmann
Jan Göttmann il 3 Set 2018
Hey thanks for your replies. I know the example wasn't very clear. I found a simple solution for the comparison issue with the standard logic ops '=='. For the Loops, I thought to complicated and changed the direction of testing, which is much simpler and works fine! Thanks!

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by