Filter löschen
Filter löschen

How to make the same length of number?

4 Ansichten (letzte 30 Tage)
tedy
tedy am 16 Apr. 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 Kommentare
tedy
tedy am 17 Apr. 2013
i can't press F12, F10, it can't works. How can i do that?
tedy
tedy am 17 Apr. 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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 17 Apr. 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 Kommentar
Jan
Jan am 17 Apr. 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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables 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