How do you concatenate bitstrings. I mean bitstrings because I do not know the number of bytes to be a multiple of 8.
A = <<3:2>>
B = <<1:1>>
C = <<15:4>>
Solution should A|B|C should be <<127:7>>
Thanks
Construct the binary using /bitstring and all the previous values. Here's an example, running in the erlang shell:
1> A = <<3:2>>.
<<3:2>>
2> B = <<1:1>>.
<<1:1>>
3> C = <<15:4>>.
<<15:4>>
4> D = <<A/bitstring, B/bitstring, C/bitstring>>.
<<127:7>>
Related
Using list comprehension, elixir allow do pattern matching like that:
iex()> for {a,2,c} = ch <- [{1,2,3},{4,5,6},3,4,5], do: c
[3]
But when I'm trying to do something like that with binary, I fail:
iex()> for << b1::size(2), b2::size(3), b3::size(3) >> = <<ch>> <- 'hello', do: b1
[]
Nevertheless, it matches well when standalone:
<< b1::size(2), b2::size(3), b3::size(3) >> = <<100>>
"d"
iex(282)> b2
4
iex(283)> b1
1
iex(284)> b3
4
It also works well when I pass mattern matching clause as secornd parameter to for:
iex(286)> for ch <- 'hello', << b1::size(2), b2::size(3), b3::size(3) >> = <<ch>>, do: b1
[1, 1, 1, 1, 1]
I'm interested if it possible to do something like first example with binary.
This is what fails:
<<ch>> <- 'hello'
In your very first example you do var <- list and later you try <<var>> <- list which is not the same by all means.
'hello' is the list of integers in the first place. Check this:
[104,101,108,108,111]
#⇒ 'hello'
Kernel.SpecialForms.for/1 is iterating through the list, one by one. One cannot match binary to an integer as is:
<<_::size(2), _::size(3), _::size(3)>> = 101
#⇒ ** (MatchError) no match of right hand side value: 101
And also:
<<ch>> = 101
#⇒ ** (MatchError) no match of right hand side value: 101
The latter code example from your question works because you match to an integer and then explicitly tell to Elixir/Erlang it’s to be treated as binary by wrapping it with << >>:
<<b1::size(2), _::size(3), _::size(3)>> = <<101>>
#⇒ "e"
I am relatively new to octave, so my question is: how can I read each line containing numbers from a file without knowing their length?
Line lengths vary.
I only know how many lines there are.
Each line has two or more float values, so I can't use "fgetl" because that will mean I will get a string, but I need an array.
There are many options and depends hoe you want to have your data stored. One way:
yourfile:
3.14 5.2 6.4
1.2 8.4
9.2
10.5 12.4
The code
fid = fopen ("yourfile", "r");
while ((tmp = fgetl (fid)) != -1)
C = strread (tmp, "%f")
#process C here
endwhile
fclose (fid);
gives:
C =
3.1400
5.2000
6.4000
C =
1.2000
8.4000
C = 9.2000
C =
10.500
12.400
I have a struct in Octave that contains some big arrays.
I'd like to know the names of the fields in this struct without having to look at all these big arrays.
For instance, if I have:
x.a=1;
x.b=rand(3);
x.c=1;
The obvious way to take a gander at the structure is as follows:
octave:12> x
x =
scalar structure containing the fields:
a = 1
b =
0.7195967 0.9026158 0.8946427
0.4647287 0.9561791 0.5932929
0.3013618 0.2243270 0.5308220
c = 1
In Matlab, this would appear as the more succinct:
>> x
x =
a: 1
b: [3x3 double]
c: 1
How can I see the fields/field names without seeing all these big arrays?
Is there a way to display a succinct overview (like Matlab's) inside Octave?
Thanks!
You might want to take a look at Basic Usage & Examples. There's several functions mentioned that sound like they'll control the displaying in the terminal.
struct_levels_to_print
print_struct_array_contents
These two functions sound like they're do what you want. I tried both and couldn't get the 2nd one to work. The 1st function changed the terminal output like so:
octave:1> x.a=1;
octave:2> x.b=rand(3);
octave:3> x.c=1;
octave:4> struct_levels_to_print
ans = 2
octave:5> x
x =
{
a = 1
b =
0.153420 0.587895 0.290646
0.050167 0.381663 0.330054
0.026161 0.036876 0.818034
c = 1
}
octave:6> struct_levels_to_print(0)
octave:7> x
x =
{
1x1 struct array containing the fields:
a: 1x1 scalar
b: 3x3 matrix
c: 1x1 scalar
}
I'm running a older version of Octave.
octave:8> version
ans = 3.2.4
If I get a chance I'll check that other function, print_struct_array_contents, to see if it does what you want. Octave 3.6.2 looks to be the latest version as of 11/2012.
Use fieldnames ()
octave:33> x.a = 1;
octave:34> x.b = rand(3);
octave:35> x.c = 1;
octave:36> fieldnames (x)
ans =
{
[1,1] = a
[2,1] = b
[3,1] = c
}
Or you you want it to be recursive, add the following to your .octaverc file (you may want to adjust it to your preferences)
function displayfields (x, indent = "")
if (isempty (indent))
printf ("%s: ", inputname (1))
endif
if (isstruct (x))
printf ("structure containing the fields:\n");
indent = [indent " "];
nn = fieldnames (x);
for ii = 1:numel(nn)
if (isstruct (x.(nn{ii})))
printf ("%s %s: ", indent, nn{ii});
displayfields (x.(nn{ii}), indent)
else
printf ("%s %s\n", indent, nn{ii})
endif
endfor
else
display ("not a structure");
endif
endfunction
You can then use it in the following way:
octave> x.a=1;
octave> x.b=rand(3);
octave> x.c.stuff = {2, 3, 45};
octave> x.c.stuff2 = {"some", "other"};
octave> x.d=1;
octave> displayfields (x)
x: structure containing the fields:
a
b
c: structure containing the fields:
stuff
stuff2
d
in Octave, version 4.0.0 configured for "x86_64-pc-linux-gnu".(Ubuntu 16.04)
I did this on the command line:
print_struct_array_contents(true)
sampleFactorList % example, my nested structure array
Output: (shortened):
sampleFactorList =
scalar structure containing the fields:
sampleFactorList =
1x6 struct array containing the fields:
var =
{
[1,1] = 1
[1,2] =
2 1 3
}
card =
{
[1,1] = 3
[1,2] =
3 3 3
}
To disable/get back to the old behaviour
print_struct_array_contents(false)
sampleFactorList
sampleFactorList =
scalar structure containing the fields:
sampleFactorList =
1x6 struct array containing the fields:
var
card
val
I've put this print_struct_array_contents(true) also into the .octaverc file.
Here is the Pseudocode for Lempel-Ziv-Welch Compression.
pattern = get input character
while ( not end-of-file ) {
K = get input character
if ( <<pattern, K>> is NOT in
the string table ){
output the code for pattern
add <<pattern, K>> to the string table
pattern = K
}
else { pattern = <<pattern, K>> }
}
output the code for pattern
output EOF_CODE
I am trying to code this in Lua, but it is not really working. Here is the code I modeled after an LZW function in Python, but I am getting an "attempt to call a string value" error on line 8.
function compress(uncompressed)
local dict_size = 256
local dictionary = {}
w = ""
result = {}
for c in uncompressed do
-- while c is in the function compress
local wc = w + c
if dictionary[wc] == true then
w = wc
else
dictionary[w] = ""
-- Add wc to the dictionary.
dictionary[wc] = dict_size
dict_size = dict_size + 1
w = c
end
-- Output the code for w.
if w then
dictionary[w] = ""
end
end
return dictionary
end
compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
print (compressed)
I would really like some help either getting my code to run, or helping me code the LZW compression in Lua. Thank you so much!
Assuming uncompressed is a string, you'll need to use something like this to iterate over it:
for i = 1, #uncompressed do
local c = string.sub(uncompressed, i, i)
-- etc
end
There's another issue on line 10; .. is used for string concatenation in Lua, so this line should be local wc = w .. c.
You may also want to read this with regard to the performance of string concatenation. Long story short, it's often more efficient to keep each element in a table and return it with table.concat().
You should also take a look here to download the source for a high-performance LZW compression algorithm in Lua...
I want to use variable inside <<>> for binary as follows:
(emacs#yus-iMac.local)56> Message = "aaa".
"aaa"
(emacs#yus-iMac.local)57> C = <<Message>>.
** exception error: bad argument
(emacs#yus-iMac.local)58> C = <<"aaa">>.
<<"aaa">>
First is error, the second is ok. Why?
Maybe stupid question.
<<"aaa">> is syntax sugar for <<$a,$a,$a>> there is not any support for direct translating list into binary in bitsyntax.
You would have to use list_to_binary/1. Like:
1> Message = "aaa".
"aaa"
2> C = list_to_binary(Message).
<<"aaa">>