I need to combine the binary representation of 6 and 7 together:
bin1 = fliplr(de2bi(6));
bin2 = fliplr(de2bi(7));
bin1 =
1 1 0
bin2 =
1 1 1
after the combination the number should be
bin3 = 110111
Does anyone have any idea on how to do this?
As suggested you can just concatenate them
bin3 = [bin1, bin2]
However, if you really want them packed together without spaces you can do it like this:
bin3 = num2str([bin1, bin2]);
bin3 = bin3(bin3 ~= ' ')
If you want to turn them in to a number now you can use str2num()
Related
I have a structure myS with several fields, including myField, which in turns includes several other fields such as BB. I need to count how many time *'R_value' appears in BB.
I have tried:
sum(myS.myField.BB = 'R_value')
and this:
count = 0;
for i = 1:numel(myS.myField)
number_of_element = numel(myS.myField(i).BB)=='R_value'
count = count+number_of_element;
end
but it doesn't work. Any suggestion?
If you are just checking if BB is that literal string, then your loop is just:
count = 0;
for i = 1:numel(myS.myField)
count = count+strcmp(myS.myField(i).BB,'R_value')
end
numel counts how many elements are. Zero is an element. so is False. Just sum the array.
count = 0;
for i = 1:numel(myS.myField)
number_of_element = sum(myS.myField(i).BB==R_value)
count = count+number_of_element;
end
Also note you had the parenthesis wrong, so you where counting how many BB where in total, then comparing that number to R_value. I am assuming R_value is a number.
e.g.:
myS.myField(1).BB=[1 2 3 4 1 1 1]
myS.myField(2).BB=[4 5 65 1]
R_value=1
I want to list all numbers from one to another but I have no idea how. For example, I want the input to be something like 2 and -3 and for the output to be 2 1 0 -1 -2 -3.
for i in reversed(range(b, a+1)):
print(i)
i = i-1
Assuming you want the integer numbers, you can do something like this:
a = int(input('first number: '))
b = int(input('second number: '))
if a > b:
a, b = b, a
for i in range(a, b+1):
print(i)
I am new in thinking about binary numbers. I'm wondering if there is a way to encode 2 4-bit numbers (i.e. hex-encoded numbers) into 1 8-bit number. So if I had a and 5 as the hex numbers, that would be 10 and 5. Maybe there is a way to store that in 1 8 bit number, in such a way that you can get it out of the 8-bit number back into its component 4-bit parts.
[10, 5]! = 15
15! = [10, 5]
Wondering if there is such a way to encode the numbers to accomplish this.
It seems like it is possible, because the first value could be stored in the first 16 digits, then the next value could be stored in the remaining, using 16 as 1, 32 as 2, 48 as 3, etc.
Can't tell if the answer here is how to do it:
How can i store 2 numbers in a 1 byte char?
Not really giving what I'd want:
> a = 10
10
> b = 5
5
> c = a + b
15
> d = (c & 0xF0) >> 4
0
> e = c & 0x0F
15
Maybe I'm not using it right, not sure. This seems like it could be it too but I am not quite sure how to accomplish this in JavaScript.
How to combine 2 4-bit unsigned numbers into 1 8-bit number in C
Any help would be greatly appreciated. Thank you!
I think the first post has the key.
Having a and 5 as the two 4-bit hex numbers to store, you can store them in a variable like:
var store = 0xa5;
or dynamically
var store = parseInt('0x' + ('a' + '9'), 16);
Then to extract the parts:
var number1 = ((store & 0xF0) >> 4).toString(16)
var number2 = ((store & 0x0F)).toString(16)
I hope this helps.
Yes this is supported in most programming languages. You have to do bitwise manipulation. The following is an example in Java.
To encode (validate input beforehand)
byte in1 = <valid input>, in2 = <valid input>;
byte out = in1<<4 | in2;
To decode:
byte in = <valid input>;
byte out1 = in>>4;
byte out2 = in & 0x0f;
i want to compare [1*232] cells of strings containing individual words from text document with [1*23] cells that contain individual sentences from the same text, can any one help me how to program it in Matlab?
for example:
"pollution" and "trees" are two words in separate cells
and following are the two sentences in separate cells:
1. trees reduce pollution.
2. trees prevent floods.
what i want to do is put 0 or 1 after comparing pollution and trees with both the sentences or in my case "n" sentences and put 1's and 0's in the form of matrices.
any help will be appreciated.
You can use a combination of cellfun and strfind. Here is a try:
Sentences = {'trees reduce pollution' ; ...
'trees prevent floods' ; ...
'pollution is bad' ; ...
'flood is worse'};
Words = {'trees', 'pollution', 'bad'};
Out = NaN(numel(Sentences), numel(Words));
for i = 1:numel(Words)
Out(:,i) = cellfun(#(x) numel(strfind(x, Words{i})), Sentences);
end
And Out contains:
Out =
1 1 0
1 0 0
0 1 1
0 0 0
Hope this helps.
I have a struct, which has 2 fields: time and pose. I have multiple instances of this struct composed in an array, so an example of this is:
poses(1)
-time = 1
-pose = (doesn't Matter)
poses(2)
-time = 2
-pose = (doesn't Matter)
poses(3)
-time = 3
-pose = (doesn't Matter)
...
Now when I print this:
poses.time
I get this:
ans =
1
ans =
2
ans =
3
How can I take that output and put it into a vector?
Use brackets:
timevec=[poses.time];
tricky matlab, I know I know, you'll just have to remember this one if you're working with structs ;)
For cases that the field values are vectors (of same size) and you need the result in a matrix form:
posmat = cell2mat({poses.pose}');
That returns each pose vector in a different row of posmat.