more efficient way of checking if conditions are satisfied?

3 Ansichten (letzte 30 Tage)
kwn500
kwn500 am 18 Dez. 2016
Kommentiert: dpb am 19 Dez. 2016
Hi, i have a stimulus that can change in three variables, in any combination (e.g no change, 1 change, 2 changes or all three changes co-occur) and for each trial i want to categorise the change that occurred for later recording in an excel spreadsheet.
my code, which currently works fine, seems too long and a bit like overkill. I was just wondering if anyone could suggest a more efficient method of checking these statements?
thanks
code below-
if orientation_seq{k} == phi && contrast_seq{k} == C && ram_seq{k} == A;
num_changes = 0; type_changes = 'no_change'; co_occur = 'no';
elseif orientation_seq{k} ~= phi && contrast_seq{k} == C && ram_seq{k} == A;
num_changes = 1; type_changes = 'orientation'; co_occur = 'no';
elseif orientation_seq{k} == phi && contrast_seq{k} ~= C && ram_seq{k} == A;
num_changes = 1; type_changes = 'contrast'; co_occur = 'no';
elseif orientation_seq{k} == phi && contrast_seq{k} == C && ram_seq{k} ~= A;
num_changes = 1; type_changes = 'RAM'; co_occur = 'no';
elseif orientation_seq{k} ~= phi && contrast_seq{k} ~= C && ram_seq{k} == A;
num_changes = 2; type_changes = 'orientation & contrast'; co_occur = 'yes';
elseif orientation_seq{k} ~= phi && contrast_seq{k} == C && ram_seq{k} ~= A;
num_changes = 2; type_changes = 'orientation & RAM'; co_occur = 'yes';
elseif orientation_seq{k} == phi && contrast_seq{k} ~= C && ram_seq{k} ~= A;
num_changes = 2; type_changes = 'contrast & RAM'; co_occur = 'yes';
elseif orientation_seq{k} ~= phi && contrast_seq{k} ~= C && ram_seq{k} ~= A;
num_changes = 3; type_changes = 'orientation & contrast & RAM'; co_occur = 'yes';
end

Akzeptierte Antwort

dpb
dpb am 18 Dez. 2016
Bearbeitet: dpb am 18 Dez. 2016
Try something like
concur={'no' 'yes}; % table of concurrence answers
ref=[phi C A]; % vector of reference conditions
inp=[orientation_seq{k} contrast_seq{k} ram_seq{k}]; % and the inputs less verbosely
changes=[inp-ref]~=0; % logical of nonzero locations
num_changes=sum(changes); % how many
co_occur = concur((num_changes>1)+1); % concurrent?
...
Store your other conditions in other lookup tables and build the other variables based on position in the changes vector.
ADDENDUM
The prime advantage of the above is that it is much more easily adapted to changing the number of inputs; only the data tables need changing, basically.
ADDENDUM(2)
Got to thinking that the latter above suggestion isn't necessarily all that clear as to how to implement...so the idea is as follows...
>> types={'orientation' 'contrast' 'RAM'}; % the non-zero types array
Having computed the differences as above so have the changes logical vector and number, then for a sample case where
>> changes=[1 1 0]~=0; % a test vector where first two are changed...
>> num_changes=sum(changes); % recompute number
>> fmt=[repmat('%s & ',1,num_changes-1) '%s']; % dynamic format string
>> type_changes = sprintf(fmt,types{changes}) % build the variable
type_changes =
orientation & contrast
>>
The dynamic format string to concatenate the pieces with the desired punctuation between and the use of the curlies "[]" on the array subscript inside the sprintf call which creates a comma-separated list that can be handled as an input vector are the two tricky parts...
Note also that the above is done for the case of having at least one change; as IA suggested, preset the defaults for no changes then after compute changes vector and number then just an
if num_changes
...
end
block for the above for the two compound variables is all that's needed.
  4 Kommentare
kwn500
kwn500 am 19 Dez. 2016
sorry, this took me a little while to get my head around, but i think i've got it. :) it works perfectly and was exactly what i needed- thanks so much again!
dpb
dpb am 19 Dez. 2016
"... works perfectly and was exactly what i needed-..."
So Accept the answer to at least indicate the thread is closed if nothing else...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 18 Dez. 2016
You could shorten it a bit by defining defaults in advance
num_changes = 0;
type_changes = 'no_change';
co_occur = 'no';
Then getting rid of the first if block and setting those values in the other if blocks only if they are different than the defaults.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by