Effacer les filtres
Effacer les filtres

How to make the same length of number?

4 vues (au cours des 30 derniers jours)
tedy
tedy le 16 Avr 2013
Hello,
i have numbers, its size are different each other. e.g 2 3 1 1 2 2 3 3 3, 2 2 3 2 2 ,2 2 2 2 3 3 3 1 1 1 1 1,so on. i wanna to make them have the same length. this is my code
gb=[2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 3 1 2 2 2 2 2 2 3 2 2 2 3 2 2 2 3 2 3 2 2 2 3 2 3 2 3 2 2 2 2 2 2 2 2 2 3 2 2 1 3 2 2 1 2 2 2 2 2 2 2 2 2 1 3 1 2 1 3 2 2 1 3 2 2 2 2 2 2 2 2 2 3 2 1 2 2 3 2 1 3 1 2 1 3 1 2 3 2 2 3 1 2 1 3 2 2 1 2 2 3 2 2 1 3 3 2 1 3 2 2 3 3 2 3 2 2 1 3 2 2 1 2 2 2 2 2 2 2 2 3 2 1 1 3 2 2 1 3 2 1 3 2 2 3 2 1 1 3 3 1 3 2 1 1 3 3 1 1 3 3 1 3 2 1 3 2 1 2 2 3 3 3 2 2 3 2 3 2 2 3 2 2 2 3 2 2 3 1 3 2 3 1 3 2 3 1 3 2 3 2 2 2 3 2 2 2 3 2 3 1 3 2 2 1 3 2 2 2 3 1 2 1 3 2 3 1 2 2 3 2 3 1 3 1 2 2 2 1 2 2 2 2 3 1 2 2 3 2 3 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 2 3 1 2 1 3 2 2 1 2 2 2 2 2 1 2 1 2 2 3 1 2 2 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
]
b = gb([diff(gb)~=0, true]);
f = diff(find([true,diff(gb)~=0,true]));
g=b(f~=1);
h=f(f~=1);
[a,b]=unique(g);
new_g=g(sort(b));
new_h=arrayfun(@(x) sum(h(g==x)),new_g);
normalized_frequency=[];
for i=1:length(new_h)
result=(new_h(i)/sum(new_h))*64;
normalized_frequency=[normalized_frequency result];
end
normalized_frequency;
rounding=round(normalized_frequency);
duplicate=[];
for i=1:length(new_g)
duplicate=[duplicate repmat(new_g(i),1,rounding(i))];
end
duplicate;
gb is an array which wanna be converted into length 64. maybe if you try another number, you'll get the length as you want, but here i give another example that won't get the right length. in this case gb has length 65 not 64 as i want. What should i do? please help me? Thanks in advance.
  5 commentaires
tedy
tedy le 17 Avr 2013
i can't press F12, F10, it can't works. How can i do that?
tedy
tedy le 17 Avr 2013
This is what i mean,
i give the result as comment in the code.
gb=[7 7 7 7 3 1 1 1 2 2 2 2 2 5 8 3 3 5 3 3 3 3];
b = gb([diff(gb)~=0, true]);
f = diff(find([true,diff(gb)~=0,true])); % 4 1 3 5 1 1 2 1 4
g=b(f~=1); % 7 1 2 3 3
h=f(f~=1); % 4 3 5 2 4
[a,b]=unique(g);
new_g=g(sort(b)); % 7 1 2 3
new_h=arrayfun(@(x) sum(h(g==x)),new_g); % 4 3 5 6
normalized_frequency=[];
for i=1:length(new_h)
result=(new_h(i)/sum(new_h))*10;
normalized_frequency=[normalized_frequency result]; % 2.2222 1.6667 2.7778 3.3333
end
normalized_frequency;
rounding=round(normalized_frequency); % 2 2 3 3
duplicate=[];
for i=1:length(new_g)
duplicate=[duplicate repmat(new_g(i),1,rounding(i))]; % 7 7 1 1 2 2 2 3 3 3
end
duplicate;
in this case gb can result 10 correctly, but when i try to input gb as my first post it can't be 64 as its length. What should i do?

Connectez-vous pour commenter.

Réponses (1)

Andrei Bobrov
Andrei Bobrov le 17 Avr 2013
ii = [true,diff(gb)~=0];
g = [gb(ii);diff(find([ii,true]))];
g1 = g(:,g(2,:)>1);
[a,b,c] = unique(g1(1,:));
w = accumarray(c,g1(2,:)');
f = [a;w.'];
[~,i1] = sort(b);
f = f(:,i1);
f(2,:) = cumsum(f(2,:)/sum(f(2,:)));
l = linspace(0,1,64);
[~,j2] = histc(l,[0 f(2,:)+eps(100)]);
out = f(1,j2);
  1 commentaire
Jan
Jan le 17 Avr 2013
@Andrei: This is one of the answers with the highest complexity in this forum. I do not understand what the program performs instantly. Although I cannot test it by my own, I'm convinced that it earns a vote!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by