confusion for how to use switch with multiple inputs

3 Ansichten (letzte 30 Tage)
Stephen Devlin
Stephen Devlin am 27 Apr. 2018
Kommentiert: Stephen Devlin am 30 Apr. 2018
Hi, I have a gui which has two popup windows to identify which type of data is being used. Each of the two data types comes from a different test, so the x axis measures different quantities, there are two option types, so two popups each with 2 options.
I have the code to switch and plot either one type or another into an axis and that works, but i want to be able to overlay(plot two sets of data) on a different axes, for this I must be using the same type of data. At the moment I can do it for when both inputs are using one type of data, but I do not know the logic to plot when both data types are the same AND how to determine which type they are. I have played around with some very basic data and got nowhere, then thought an XNOR would tell me when both inputs were the same, but it doesn't tell me which type.
Any ideas or does this all need rewording to clarify?
clear all
close all
clc
A=[0 0 1 1];
B=[0 1 0 1];
A=A';
B=B';
C=logical(horzcat(A,B))
for k=1:4
if C(k,1)& C(k,2) ==0
C(k,:)
disp 'all inputs low'
elseif C(k,1)& C(k,2) ==~1
C(k,:)
disp ' inputs not same'
elseif C(k,1) & (C(k,2)) ==~0
C(k,:)
disp ' inputs not same'
else C(k,1) & C(k,2) ==1;
C(k,:)
disp 'all inputs high'
end
end
D=~xor(A,B)%XNOR
It gives the following results which confuse me as it is telling me all inputs are high when the inputs are 0,1 respectively.
C =
4×2 logical array
0 0
0 1
1 0
1 1
ans =
1×2 logical array
0 0
all inputs high
ans =
1×2 logical array
0 1
all inputs high
ans =
1×2 logical array
1 0
all inputs low
ans =
1×2 logical array
1 1
inputs not same
D =
4×1 logical array
1
0
0
1

Akzeptierte Antwort

Guillaume
Guillaume am 27 Apr. 2018
Bearbeitet: Guillaume am 27 Apr. 2018
yes, none of your tests make much sense. I find your use of spaces very strange.
if C(k,1)& C(k,2) ==0
using brackets for clarity, and consistent spacing, this is equivalent to
if C(k, 1) & (C(k, 2) == 0)
which would be written more simply as
if C(k, 1) & ~C(k, 2)
hence is true when C(k, 1) is true AND C(k, 2) is false. Not what you wanted.
elseif C(k,1)& C(k,2) ==~1
~1 is 0 so this is testing exactly the same as the previous test. Why would you write ~1 in the first place, it's just a convoluted way of writing 0.
elseif C(k,1) & (C(k,2)) ==~0
Again, ~0 is just a convoluted way of writing 1. Plus we've got some brackets that don't do anything. The test is equivalent to
elseif C{k,1) & C(k, 2)
which is true when both are true. Since this is set to display 'input not same', it's clearly the wrong test.
else C(k,1) & C(k,2) ==1
is the same as the previous test.
A much simpler way to achieve the correct result with your example:
C = logical([0 0; 0 1; 1 0; 1 1]);
for row = 1:size(C, 1)
if C(row, 1) ~= C(row, 2)
disp('input not the same');
elseif C(row, 1) %guaranteed to be identical to C(row, 2)
disp('all input high');
else
disp('all input low');
end
end
And as a bonus, a vectorised version:
C = logical([0 0; 0 1; 1 0; 1 1]);
disptext = {'input not the same'; 'all input low'; 'all input high'};
disptext(1 + (C(:, 1) == C(:, 2)) + C(:, 1) .* (C(:, 1) == C(:, 2)))
edit: minor error in answer.
  4 Kommentare
Guillaume
Guillaume am 30 Apr. 2018
The loop is not really needed. This woud achieve more or less the same output:
C = logical([0 0; 0 1; 1 0; 1 1]);
disptext = {'input not the same'; 'all input low'; 'all input high'};
celldisp(disptext(1 + (C(:, 1) == C(:, 2)) + C(:, 1) .* (C(:, 1) == C(:, 2))))
The aim of the math is to generate the appropriate index into disptext, 1 for when the inputs are not the same, 2 for when they're both low, and 3 fir when they're equal.
(C(:, 1) == C(:, 2)
generates 0 when the inputs are not the same, and 1 when they are. Add 1 to that, and you get 1 when the inputs are not the same (what we want) and 2 when they're identical (what you want when they're both low). You now need to add 1 when they're both high, this is what
C(:, 1) .* (C(:, 1) == C(:, 2))
does.
Stephen Devlin
Stephen Devlin am 30 Apr. 2018
Hi Guillaume, I get it, but how did you come up with that, to me it's genius and I have no idea how you could do that as quick as you did (i have emailed myself the code as I will need it again definitely). Any pointers for what I should read up on to write lines like that?
Best regards
Steve

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by