hii i have this
f= 1 0
0 1
0 2
1 3
0 4
1 5
1 6
0 7
this indicate the numbers from 0 to 7 in decimal i want to regroupe the 1 in a matrix and at the same time tell which number it is and do the same thing with 0 i have a mistake somewhere here's the program
N=8;
r=3;
A=zeros(N,r);
Q=zeros(N,1);
for k=0:1:N-1
A(k+1,:)=dec2bin(k,r)-'0';
Q(k+1,1)=xor(xor(A(k+1,1),A(k+1,2)),A(k+1,3));
f=xor(Q,1);
end
for k=0:1:N-1
if f(k+1)==1
se=k
S(k+1,1)=se
else
ss=k
C(k+1,1)=ss
end
end
S =
0
0
0
0
3
5
0
6
C =
0
1
2
0
4
0
0
7
i don't know how to do to delete the 0 i just want 0356 and 1247 to appears
i hope you understand me

 Akzeptierte Antwort

Thorsten
Thorsten am 1 Dez. 2014
Bearbeitet: Thorsten am 1 Dez. 2014

0 Stimmen

Ok, here is my 2nd solution. There's also a faster way to compute f, I think:
f = 1 - (mod(sum(A'), 2) == 1);
x = 0:7;
s = x(~logical(f));
c = x(logical(f));

3 Kommentare

Dija
Dija am 1 Dez. 2014
Thank you so much can u ask another question please!
Thorsten
Thorsten am 1 Dez. 2014
Sure, but please start a new question to that others can help you, too.
it is about this program too and i guess that you can understand me more than others it is okey if you can't help me but i will try with you first :)
how can i discard any bianry digit i want in A for example if i discard the first one in A
A =
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
i want to get this
A =
0 0
0 1
1 0
1 1
0 0
0 1
1 0
1 1

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

dpb
dpb am 30 Nov. 2014
Bearbeitet: dpb am 30 Nov. 2014

0 Stimmen

>> s=find(f(:,1))
s =
1
4
6
7
>> c=f(s,2)
c =
0
3
5
6
>>
Looks like your desired S above is from some other sample dataset...f(2,1)==0
ADDENDUM
OK, following your comment/plea; the complementary is by the above
s=find(f(:,1);
c=f(s,2);
s=f(find(f(:,1)==0,2));
This is a little convoluted; I did it that way because as noted I thought you were looking for the position in the original vector, not the two results.
More straightforward in the latter case is to use logical addressing instead of find --
ix=f(:,1)==1); % the '1' location vector
s=f(ix,2);
c=f(~ix,2);

4 Kommentare

Dija
Dija am 30 Nov. 2014
it doesnt work i want to regroupe {0 3 5 6} together and {1 2 4 7}
dpb
dpb am 30 Nov. 2014
What rule puts [1 2 4 7] together? The positions that correspond to the value '1' in f(:,1) aren't those.
Oh; I see -- it's not the positions of the '1' entries corresponding it's the complementary values. That's simply returning the '0' rows instead of '1'. See updated Answer.
Dija
Dija am 1 Dez. 2014
it doesnt work too
Does too...
>> f
f =
1 0
0 1
0 2
1 3
0 4
1 5
1 6
0 7
>> ix=f(:,1)==1;
>> s=f(ix,2)
ans =
0
3
5
6
>> c=f(~ix,2)
c =
1
2
4
7
>>

Melden Sie sich an, um zu kommentieren.

Thorsten
Thorsten am 1 Dez. 2014

0 Stimmen

x = f(:,2);
x(logical(f(:,1)))
ans =
0
3
5
6
x(~logical(f(:,1)))
ans =
1
2
4
7

9 Kommentare

Dija
Dija am 1 Dez. 2014
i have this error when i run the program
Attempted to access f(:,2); index out of bounds because size(f)=[8,1].
Error in Untitled2 (line 12) x = f(:,2);
I used the f in your example which is of size 8 x 2:
f = [1 0
0 1
0 2
1 3
0 4
1 5
1 6
0 7];
Andrei Bobrov
Andrei Bobrov am 1 Dez. 2014
I too
oh i see i am sorry i was explaining that it goes from 0 to 7 i have a matrix
A =
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
then i calculate the xor of each digit with this program
for k=0:1:N-1
A(k+1,:)=dec2bin(k,r)-'0';
Q(k+1,1)=xor(xor(A(k+1,1),A(k+1,2)),A(k+1,3));
f=xor(Q,1);
end
and i got f
f =
1
0
0
1
0
1
1
0
Now i want to put all the 1 together and the 0 together and at the same time i want to tell which number it is
i did this program
for k=0:1:N-1
if f(k+1)==1
se=k;
s(k+1,1)=se;
else
ss=k;
c(k+1,1)=ss;
end
end
and i got this results s =
0
0
0
3
0
5
6
c =
0
1
2
0
4
0
0
7
know i want the results to be like this
s =
0
3
5
6
c = 1 2 4 7 I dont know where is my mistake
here it is all the program
N=8;
r=3;
A=zeros(N,r);
Q=zeros(N,1);
for k=0:1:N-1
A(k+1,:)=dec2bin(k,r)-'0';
Q(k+1,1)=xor(xor(A(k+1,1),A(k+1,2)),A(k+1,3));
f=xor(Q,1);
end
A
f
for k=0:1:N-1
if f(k+1)==1
se=k;
s(k+1,1)=se;
else
ss=k;
c(k+1,1)=ss;
end
end
s
c
dpb
dpb am 1 Dez. 2014
...to put all the 1 together and the 0 together and at the same time i want to tell which number it is...
That's my first solution via find
ive tried it but it didnt work i got this error Index exceeds matrix dimensions. Error in c=f(s,2)
beside there's a mistake in s s =
1
4
6
7
it is 1 2 4 7
dpb
dpb am 1 Dez. 2014
Well, you've got to use the arrays as they're sized--I can't help you changing from the initial 8x2 to something else. It's your problem, fix up the indexing to address the data where it is. The sample problem was two columns with the index to segregate over in the first and the second column containing values. If you want them some other way, adjust the logic to address that.
Dija
Dija am 1 Dez. 2014
thank you
dpb
dpb am 1 Dez. 2014
Well, again we're mis-communicating on what you mean by "which number it is" -- whether it's the location in the array or the numeric value. Since you weren't happy with the latter in the demonstrated solution I thought you meant the position again which find returns.
If it's actually the numeric value then the logic array solution is the more straightforward as demonstrated with the inputs in a 2-column array. If you don't put them in the array, then use whatever is the storage for each column in place of the array.

Melden Sie sich an, um zu kommentieren.

Andrei Bobrov
Andrei Bobrov am 1 Dez. 2014

0 Stimmen

S = num2str(f(f(:,1)>0,2))';
C = num2str(f(~f(:,1),2))';

1 Kommentar

Dija
Dija am 1 Dez. 2014
the same error :( Index exceeds matrix dimensions.
Error in Untitled2 (line 12) S = num2str(f(f(:,1)>0,2))'

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 30 Nov. 2014

Kommentiert:

dpb
am 1 Dez. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by