assign symbol for given range of data

3 views (last 30 days)
I have sequences
a=[1 5 3 5 5 2 12 7 14 2 11 11 5 7 7 3];
b=[0 0.1 3 0.8 0.8 5 5 7 7 0.56 0.56 1 1 0 3 4].
a has min 1, max 14 value and b has min 0, max 7 value. I want to make range for a and b and want to assign symbol for numbers falls in that particular range. i.e. for a range is 3 so 1-3,4-6,7-9,10-12,13-15 and for b range is 0.5 i.e. 0-0.5,0.5-1,1-1.5,.....,6.5-7. i.e.
1-3 4-6 7-9 10-12 13-15
0-0.5 p1 p2 p3 p4 p5
0.51-1 p6 p7 p8 p9 p10
1.01-1.5 p11 p13 p14 p15 p16
1.51-2 ................................
2.01-2.5 ................................
,......, ................................
6.5-7 ................................
i.e. numbers falls between a=1-3 and b=0-0.5 will assign value p1, numbers falls between a=4-6 and b=0.5-1 will assign value p2 and likewise. p1, p2,.. are symbols so they can be anything I want to take them as numbers i.e. p1=1, p2=2, p3=3,....
so here I write for 2 variables but I have data 4 variable and want to do this for 4 variable data so how to do for 4 variable data.
finally I want output which indicates that for particular number of a and b which symbol is assign i.e.
a b symbol
1 0 p1
5 0.1 p3
3 3 p7
5 0.8 .
5 0.8 .
2 5 .
12 5
7 7
14 7
2 0.56
11 0.56
11 1
5 1
7 0
7 3
3 4
so like this for 4 variable data, I used p1, p2,.... just to indicate as symbol but I want use symbol as numbers i.e. 1,2,3,.... i.e. 1,2,3,... instead p1,p2,...

Accepted Answer

Image Analyst
Image Analyst on 26 Dec 2016
Just define your ranges and have a 4-nested for loop and create a cell array
counter = 1;
for v1 = 1 : length(ranges1)
for v2 = 1 : length(ranges2)
for v3 = 1 : length(ranges3)
for v4 = 1 : length(ranges4)
% Assign the p string to the cell array.
ca{v1,v2,v3,v4} = sprintf('p%d', counter);
counter = counter + 1;
end
end
end
end
  7 Comments
Image Analyst
Image Analyst on 27 Dec 2016
Try this:
a=[1 5 3 5 5 2 12 7 14 2 11 11 5 7 7 3];
b=[0 0.1 3 0.8 0.8 5 5 7 7 0.56 0.56 1 1 0 3 4];
maxAIndex = ceil(max(a)/3)
fprintf(' a b symbol aIndex bIndex\n');
for k = 1 : length(a)
aIndex = ceil(a(k)/3);
bIndex = ceil(b(k)/0.5);
% handle special case of 0
if bIndex == 0
bIndex = 1;
end
symbol(k) = (bIndex-1) * maxAIndex + aIndex;
fprintf('%2d %.2f %3d, %8d %8d\n', a(k), b(k), symbol(k), aIndex, bIndex);
end
a b symbol aIndex bIndex
1 0.00 1, 1 1
5 0.10 2, 2 1
3 3.00 26, 1 6
5 0.80 7, 2 2
5 0.80 7, 2 2
2 5.00 46, 1 10
12 5.00 49, 4 10
7 7.00 68, 3 14
14 7.00 70, 5 14
2 0.56 6, 1 2
11 0.56 9, 4 2
11 1.00 9, 4 2
5 1.00 7, 2 2
7 0.00 3, 3 1
7 3.00 28, 3 6
3 4.00 36, 1 8
Jay Hanuman
Jay Hanuman on 28 Dec 2016
how to do for 4 variable data as
a=[1 5 3 5 5 2 12 7 14 2 11 11 5 7 7 3];
b=[0 0.1 3 0.8 0.8 5 5 7 7 0.56 0.56 1 1 0 3 4];
c=[1 0.5 5 10 10 15 12 12 26 9 16 18 18 16 20 20 18 17 22 26 23 24];
d=[10 16 51 26 23 23 52 88 54 74 74 46 1 15 12 64 56 54 45 55 16 46];

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!