How to make the same length of number?
Show older comments
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 Comments
tedy
on 17 Apr 2013
Image Analyst
on 17 Apr 2013
They're probably like me, and can't figure out what you want. No comments in the code - I hardly even bothered to look at the code. I don't even know what it means when you have an array of single digit numbers and you say you want all the numbers to be the same size or length. I have no clue at all - nada.
There is that and the 9% accept rate which might make some of us believe that not a lot of time is put then into understanding the answers (or at least accepting and voting for them).
That said, you should simplify your problem and work on a gb vector with a length of 10-20 so you can observe what happens more easily. You should also learn using the debugger so you can execute your code line by line and check what happens at every step of the computation. To help you start with this, put your cursor on the line
b = gb([diff(gb)~=0, true]);
and press F12. This sets a break point at this line (indicated with a red disk on the left of the line, that you can enable/disable by clicking on it). Now press F5; you see that MATLAB runs the code up to the break point (not included), the current location in the code being indicated by a green arrow (more precisely, the green arrow is at the beginning of the next line to be run). Pressing F10, you'll see that MATLAB will execute the current line and move the cursor (green arrow) down. Go on pressing F10 until the cursor reaches the line
new_g=g(sort(b));
Now in the command window, type a to display the content of variable a. So you see that you can step line by line into the code and display the content of variables. You can also step into functions with F11, break the debugging session with shift+F5, but I let you discover that on your own.
tedy
on 17 Apr 2013
tedy
on 17 Apr 2013
Answers (1)
Andrei Bobrov
on 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 Comment
Jan
on 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!
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!