how to construct a matrix according to the binary order?

1 Ansicht (letzte 30 Tage)
Sahar abdalah
Sahar abdalah am 21 Jun. 2015
Kommentiert: Sahar abdalah am 22 Jun. 2015
hello everyone , I have a matrix of probability: prb.mat and another matrix class.mat that contain the class for each probability like this :
prb =[0.0953 0.0116 0.0086
0.0067 0.0112 0.0092
0.0568 0.0639 0.959]
class = [1 2 3
2 3 1
3 2 1]
I want to construct a matrix m using the binary order according to the class of probability like this :
class 3 class 2 class 1 order class position in m
0 0 0 ==> ensemble vide {} 1
0 0 1 ==> {class 1} 2
0 1 0 ==> {class 2} 3
0 1 1 ==> {class 1,class 2} 4
1 0 0 ==> {class 3} 5
1 0 1 ==> {class 1,class 3} 6
1 1 0 ==> {class 2,class 3} 7
1 1 1 ==> {class 1,class 2,,class 3} 8
the matrix m (size 2^n) is constructed for each row of each matrix prb.mat and class.mat as follows for the first line of prb and class :
prb = 0.0953 0.0116 0.0086
order = 1 2 3
n=3;
m(1,2,3)=i.*prob(i);==> position in m=8
m(1,2)=i.*prob(i);===> position in m=4
m(1)=i.*prob(i); ===> position in m=2
and the other values are 0. then the final matrix m for the fist line is
m=[0 0,0953 0 0,0232 0 0 0 0,0258]
I doing this using this code :
prb =[0.0953 0.0116 0.0086
0.0067 0.0112 0.0092
0.0568 0.0639 0.959]
class = [1 2 3
2 3 1
3 2 1]
n=size(prb,1);
for i=1
prob1=prb(i,:)
indice=class(i,:)
end
%%%%%%%%%%%%construction m %%%%%%%%%%%%%%%%%
ind = 2^indice(1); %%ind: position du 1er élément
m=[];
for i=1:n-1
m(ind)= i.*prob1(i);
ind=2^indice(i+1) ;
end
ind=2^(n);
m(ind)=n*prob1(n);
now the problem is that this code works for the first line for prb and class matrix and does not work for the other lines. the problem exactly is that the values are not set in their good position in the matrix m. for example, this code gives a wrong result for the second line for i = 3
prob=[0.0568 0.0639 0.959]
order=[ 3 2 1]
m=[0 0 0 0,1278 0 0 0 2,8770]
but the good positions are m are:
m(3,2,1)= 3.*prob(i); position in m=8
m(3,2)= 2.* prob(i);position in m=7
m(3)= 1 .*prob(i); position in m=5
please help me to change this program to obtain each value in their good position in m following the binary order. I await suggestions and thank you in advance

Akzeptierte Antwort

Guillaume
Guillaume am 21 Jun. 2015
If I understood correctly, this should work:
%note that I'm using clss instead of class for your class variable
%as class is already used by matlab for a function
assert(size(prb) == size(clss)); %always a good idea
m = zeros(size(prb, 1), 2 .^ size(prb, 2));
for row = 1 : size(prb, 1)
indices = cumsum(2 .^ (clss(row, :) - 1)) + 1;
m(row, indices) = prb(row, :) .* clss(row, :);
end

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices 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