Vector problem
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I've got a problem, and I hope that someone here can help me. Consider the following vector 'v'.
0 0
1 0
1 0
1 1
1 0
I have to create a third column to this vector, and the value of that column has to depend on the combination of column 1 and 2. If the combination is (1,1), like in the fourth row, the value of the third column has to be 1. If the combination is (0,0), like in the first row, the value third column has to be 0.
The problem arises when the combination is (1,0). In this situation, we have to look at the row before a consecutive series of (1,0). If before the (1,0)series, the combination is (0,0), the value of the third column of the (1,0) combination has to be 0. If before the (1,0)series the combination is (1,1), the value has to be 1.
The outcome should be the following:
0 0 0
1 0 0
1 0 0
1 1 1
1 0 1
Does anyone know how I can program this in Matlab? Thanks,
Pieter
1 Commento
Matt Fig
il 31 Mar 2011
BTW, and for future reference, a N-by-2 variable is not a vector unless N is 1. You have got yourself a matrix, or more generally, an array. A vector has all of its dimension sizes equal to 1 except one dimension.
Risposta accettata
Jan
il 31 Mar 2011
Very similar to Paulo's implementation, but faster on my computer:
a = [0 0; 1 0; 1 0; 1 1; 1 0];
function c = AddColumn(a)
n = size(a, 1);
b = zeros(n, 1);
e = a(:, 1) == a(:, 2); % Equal elements
b(e) = a(e, 1);
q = 0; % What is wanted if first row is [1, 0]?
for i = 1:n
if e(i)
q = b(i);
else
b(i) = q;
end
end
c = [a, b];
And a vectorized approach:
a = [0 0; 1 0 ; 1 0 ; 1 1 ; 1 0];
function c = AddColumn(a)
n = size(a, 1);
aEq = a(:, 1) == a(:, 2); % Equal elements
aFull = a(aEq, 1);
c = [a, aFull(cumsum(aEq)];
2 Commenti
Paulo Silva
il 31 Mar 2011
That vectorized approach is awesome, I had to spend a couple of minutes to learn how it worked so fine :)
Più risposte (3)
Paulo Silva
il 31 Mar 2011
a=[ 0 0; 1 0 ; 1 0 ; 1 1 ; 1 0] %inputs
c=zeros(size(a,1),3); %pre-allocate table
c(:,1:2)=a; %put the inputs on the table
c(1,3)=a(1,1) & a(1,2); %first value doesnt depend on the last one
%the other values depend on the last output
for b=2:size(a,1) %for every line starting on the second
c(b,3)=(a(b,1) & a(b,2)) | c(b-1,3); %compute the rest of the values
end
c; %your truth table
Just for my own record here's my version that works
%a=[ 1 1; 1 0 ; 1 0 ; 1 0 ; 1 0] %inputs
a=[ 0 0; 1 0 ; 1 0 ; 1 1 ; 1 0] %inputs
%a=randi([0 1],2870,2); %user situation
%tic
c=zeros(size(a,1),3); %pre-allocate table
c(:,1:2)=a; %put the inputs on the table
b=1:size(a,1);
c(b,3)=a(b,1) & a(b,2); %find all 1 1 situations and give output 1
%the other values depend on the last output
%find those special 1 0 situations
for b=2:size(a,1)
d=0;
%find if special situation occurs and find the result before consecutive
%series
while ((a(b-d,1)==1) & (a(b-d,2)==0) & ((b-d)>1))
d=d+1;
end
c(b,3)=c(b-d,3); %compute the rest of the values
end
c %your truth table
%toc %shouldnt take long
4 Commenti
Sean de Wolski
il 31 Mar 2011
Paulo's example is generalized and should work for any array you have of size nx2 called 'a'.
Paulo Silva
il 31 Mar 2011
I checked the code, it doesn't work as required, my mistake, Jan's answer is working just fine.
Wolfgang Schellenberger
il 31 Mar 2011
Why not c=abs(a(:,1)-a(:,2)) ?
2 Commenti
Paulo Silva
il 31 Mar 2011
it gives the wrong answer for the first two inputs 1 0 and doesn't take into account the last output value.
Sean de Wolski
il 31 Mar 2011
-1 Vote!
|1-1| = 0; which yields the wrong result. It also doesn't account for previous rows.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!