Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

trying to compare between matrixes

1 vue (au cours des 30 derniers jours)
itsik
itsik le 7 Mai 2011
Clôturé : MATLAB Answer Bot le 20 Août 2021
[EDIT: 20110512 23:42 CDT - incorporate duplicate post - WDR]
hello!!!
I'm trying to compare between 2 matrices the first matrix is 8X4 so there is 8 users that gets a random variables '0' or '1' so I'm trying to compare all the lines to the maximum combinations (2^4)
this is part of my program but its doesn't running i would like to read yours comments about it and maybe someone can help me to solve this problem
thank u very much!
this is the program (i wrote only 2 lines of the compare but i might sure u understand the idea... :D ) and as u can see if the compare is right then C will get some number if not it will jump to the other line
clear all
for i=1:4 %data
for q=1:8 %users
a=rand(q,i)
if (a(i) <=0.5)
b(q,i)=0
else
if( a(i) >0.5)
b(q,i)=1
end
end
end
end
for q=1:8
if b(q,1:4)~={[0,0,0,1]|[0,0,1,0]|[0,0,1,1]|[0,1,0,0]|[0,1,0,1]|[0,1,1,0]|[0,1,1,1]|[1,0,0,0]|[1,0,0,1]|[1,0,1,0]|[1,0,1,1]
[1,1,0,0]|[1,1,0,1]|[1,1,1,0]|[1,1,1,1]}
c=pol2cart(0,1)
else
if b(q,1:4)~={[0,0,0,0]|[0,0,1,0]|[0,0,1,1]|[0,1,0,0]|[0,1,0,1]|[0,1,1,0]|[0,1,1,1]|[1,0,0,0]|[1,0,0,1]|[1,0,1,0]|[1,0,1,1]
[1,1,0,0]|[1,1,0,1]|[1,1,1,0]|[1,1,1,1]}
c=pol2cart(pi/8,1)
EDITOR: the duplicate post expanded the "if" cascade to:
for q=1:8
if b(q,1:4)~={[0,0,0,1]|[0,0,1,0]|[0,0,1,1]|[0,1,0,0]|[0,1,0,1]|[0,1,1,0]|[0,1,1,1]|[1,0,0,0]|[1,0,0,1]|[1,0,1,0]|[1,0,1,1]
[1,1,0,0]|[1,1,0,1]|[1,1,1,0]|[1,1,1,1]}
c=pol2cart(0,1)
else
if b(q,1:4)~={[0,0,0,0]|[0,0,1,0]|[0,0,1,1]|[0,1,0,0]|[0,1,0,1]|[0,1,1,0]|[0,1,1,1]|[1,0,0,0]|[1,0,0,1]|[1,0,1,0]|[1,0,1,1]
[1,1,0,0]|[1,1,0,1]|[1,1,1,0]|[1,1,1,1]}
c=pol2cart(pi/8,1)
else
if b(q,1:4)~={[0,0,0,1]|[0,0,0,0]|[0,0,1,1]|[0,1,0,0]|[0,1,0,1]|[0,1,1,0]|[0,1,1,1]|[1,0,0,0]|[1,0,0,1]|[1,0,1,0]|[1,0,1,1]
[1,1,0,0]|[1,1,0,1]|[1,1,1,0]|[1,1,1,1]}
c=pol2cart(2*pi/8,1)
else
if b(q,1:4)~={[0,0,0,1]|[0,0,1,0]|[0,0,0,0]|[0,1,0,0]|[0,1,0,1]|[0,1,1,0]|[0,1,1,1]|[1,0,0,0]|[1,0,0,1]|[1,0,1,0]|[1,0,1,1]
[1,1,0,0]|[1,1,0,1]|[1,1,1,0]|[1,1,1,1]}
c=pol2cart(3*pi/8,1)
else.
.
.
.
.
.
[1,1,0,0]|[1,1,0,1]|[0,0,0,0]|[1,1,1,1]}
c=pol2cart(14*pi/8,1)
else
if b(q,1:4)~={[0,0,0,1]|[0,0,1,0]|[0,0,1,1]|[0,1,0,0]|[0,1,0,1]|[0,1,1,0]|[0,1,1,1]|[1,0,0,0]|[1,0,0,1]|[1,0,1,0]|[1,0,1,1]
[1,1,0,0]|[1,1,0,1]|[1,1,1,0]|[0,0,0,0]}
c=pol2cart(15*pi/8,1)
end
end
end

Réponses (2)

bym
bym le 7 Mai 2011
I don't think you are initializing 'a' properly. Try this:
a=rand(8,4);
a(a<=.5)=0;
a(a>.5)=1;

Walter Roberson
Walter Roberson le 7 Mai 2011
Comparing a numeric array to a cell array will give you an error. You need something like,
if ismember(b(q,1:4), [0,0,0,1;0,0,1,0;0,0,1,1;0,1,0,0;0,1,0,1;0,1,1,0;0,1,1,1;1,0,0,0;1,0,0,1;1,0,1,0;1,0,1,1;1,1,0,0;1,1,0,1;1,1,1,0;1,1,1,1], 'rows')
  4 commentaires
Walter Roberson
Walter Roberson le 8 Mai 2011
Looking at your other post for the values you wish to use, the code reduces to
a = rand(8,4) > 0.5;
c = pol2cart(Pi/8 * sum(bsxfun(@times,a,[8 4 2 1]),2), ones(size(a,1),1));
Walter Roberson
Walter Roberson le 8 Mai 2011
As to what ismember() and rows means in context, here is the link to the documentation: http://www.mathworks.com/help/techdoc/ref/ismember.html

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by