Could you help me? I have n = 10 (ten tags) with 8 bits value each. Every tag should have one randomly created 1's in ID (for example 00000100, 01000000). How can I do this in Matlab?
Asked
Active
Viewed 62 times
-3
-
You need to show some code and clarify your question. As it is written this question should be closed as it is not providing a clear example of the problem – PyNEwbie Nov 23 '15 at 14:29
-
number_tags= 10. a want to create 10 vectors with 8 bits. Also I want to randomly choose number 1 in each tag. For example: Tag1 = [00100000]; Tag2 = [00000100]; Tag3 = [01000000]; – NikolaC Nov 23 '15 at 14:31
-
How about you create a [random integer](http://nl.mathworks.com/help/stats/random.html) and [turn it into a binary number](http://nl.mathworks.com/help/fixedpoint/ref/bin.html) – Johannes Nov 23 '15 at 14:34
-
I can not do it. If you can write a code – NikolaC Nov 23 '15 at 14:39
1 Answers
0
Let's try this:
n = 10;
r = 8;
k = randi(r,1,n);
Tag = zeros(r,n);
Tag(r*(find(k)-1) + k)=1;
Tag = Tag';
So:
k =
8 8 5 2 2 3 7 3 7 2
Tag =
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0
Now each row - your Tag. For example, Tag1 = Tag(1,:)
.
In this case lets find needed result: if we need only logical values (1 if there is 1 in any row, and 0 if there is no any 1 in column) we have to add this:
result = sum(Tag);
result(find(result))=1
result =
0 1 1 0 1 0 1 1
Number of ones and zeros:
c1 = sum(result);
c0 = numel(result) - c1;

Mikhail_Sam
- 10,602
- 11
- 66
- 102
-
I want for bigger number of tags. And as you can see does not working for Tag3 – NikolaC Nov 23 '15 at 14:53
-
In your question `n=10` so it works. But ok i will make it for any n. For `Tag3` works too, of course, if you add `Tag3 = zeros(1,8)` similarly. – Mikhail_Sam Nov 23 '15 at 14:56
-
Friend, could you see this: clear all clc; c = 16; r = 8; n= 10; c = zeros(n,c); I want to every row in this matric has one random 1 on some position. OK? – NikolaC Nov 23 '15 at 15:14
-
-
-
-
I wrote `every column` ) ok, if you need rows just use `Tag = Tag';` after all and you'll get it! – Mikhail_Sam Nov 24 '15 at 06:24
-
But if I do Tag = Tag'; I change size of matrix. Now is 10x8 instead 8x10 – NikolaC Nov 24 '15 at 10:53
-
-
OK. Thank you. One more question. @Mikhail_Sam how can i calculate final bits. Please look example: [0 0 0 0 0 0 0 0 0 1 0 0] [0 1 0 0 0 0 0 0 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 0 0] Result: [0 1 0 0 0 0 0 0 0 1 0 0] – NikolaC Nov 24 '15 at 11:11
-
-
-
Great @Mikhail_Sam. I want one more thing. How to calculate number of '1' and '0' in result? – NikolaC Nov 24 '15 at 11:36
-
-
-
-