Mapping algorithm in C - c

scala can make mapping (ADT) so we can mapping like this ('A', 3) = (Char, Int)
how about in C?
I want to mapping and check all the relations and comparing between two maps
'a' = 1, 'b' = 3, 'c' = 4 is mapping by abbbcccc
and 'e' = 1 , 'b' = 3, 'g' = 4 is mapping by bbbegggg
I want to find these relation ('a' , 1) is not in ('e' = 1 , 'b' = 3, 'g' = 4). then, this map
('b' = 3) is in ('e' = 1 , 'b' = 3, 'g' = 4) this map. and count++;
How I can make these like maps? can I make it by array?

Not in plain C, no.
You could implement one with an array or two, but you would have to implement either a hashing algorithm, or some kind of comparison and search algorithm. Alternatively you could use some kind of search tree to implement it.
If you don't want to write a map data type, you will have to use a library with that functionality. GLib contains one: http://developer.gnome.org/glib/2.30/glib-Hash-Tables.html

Related

Using one array to index another in Ruby

I am trying to learn Ruby, and I'm wondering how an array can be used to index another array, for example,
in Perl this is: my #x = #y[#ro], where all three variables are just generic arrays.
how can I accomplish the same thing in Ruby?
If I remember my Perl correctly, given:
my #ro = ('a', 'b', 'c', 'd', 'e');
my #y = (1, 3);
Then #ro[#y] would be ('b', 'd') so the notation is just a short form for extracting all the elements of the array #ro at the indexes in #y.
In Ruby, I'd use Array#values_at and a splat thusly:
ro = %w[a b c d e]
y = [1, 3]
x = ro.values_at(*y)
The *y splat unwraps the array and gives you its elements so ro.values_at(*y) is equivalent to ro.values_at(1, 3) in this case.

MATLAB cellfun() to map contains() to cell array

a={'hello','world','friends'};
I want to see if for every word in the cell array contains the letter 'o', how to use cellfun() to achieve the following in a compact expression?
b = [ contains(a(1),'o') contains(a(2),'o') contains(a(3),'o')]
You don't need cellfun, if you read the documentation, contains works natively on cell arrays of characters:
a = {'hello', 'world', 'friends'};
b = contains(a, 'o');
Which returns:
b =
1×3 logical array
1 1 0

MATLAB Array of structures assignment

I have an array of structures. Lets say
s(1).value, ... , s(5).value.
I have a vector of values, lets say vals = [1 2 3 4 5], that i want to assign to the array of structures. So written in pseudocode i want: s(:).value = vals.
As shown below there is a know solution. But is it really not possible to do this assignment in 1 line as in the pseudocode?
% Vector of values
vals = [1 2 3 4 5];
n = length(vals);
% Initialize struct
s(n).values = 0;
% Put vals into my struct.values
[s(1:n).values] = ???
% Known solution that i am not satisfied with:
vals_c = num2cell(vals);
[s(1:n).values] = vals_c{:};
Best regards, Jonas
It's possible to do this in one line using cell2struct in conjuction with num2cell.
% Vector of values
vals = [1 2 3 4 5];
n = length(vals);
% Put vals into my struct.values
s = cell2struct(num2cell(vals), 'values', 1)
% transpose if orientation is important
s = s.';
it's not pretty, but it does do it in one line. cell2struct supports multiple entries so you may be able to populate many fields.
The big downside is that it creates the struct from scratch, so you'd have to do a struct merge if you need to add this data to an existing struct.
Having recently gone through the same phase I thought I'd answer this one.
To create a new structure with one field:
field = 'f';
value = {'some text';
[10, 20, 30];
magic(5)};
s = struct(field,value)
Create a nonscalar structure with several fields:
field1 = 'f1'; value1 = zeros(1,10);
field2 = 'f2'; value2 = {'a', 'b'};
field3 = 'f3'; value3 = {pi, pi.^2};
field4 = 'f4'; value4 = {'fourth'};
s = struct(field1,value1,field2,value2,field3,value3,field4,value4)
Also, as I'd always suggest, going over the documentation a few times is quite necessary and useful, so there you go. https://in.mathworks.com/help/matlab/ref/struct.html

Finding an element of a structure based on a field value

I have a 1x10 structure array with plenty of fields and I would like to remove from the struct array the element with a specific value on one of the field variables.
I know the value im looking for and the field I should be looking for and I also know how to delete the element from the struct array once I find it. Question is how(if possible) to elegantly identify it without going through a brute force solution ie a for-loop that goes through elements of the struct array to compare with the value I m looking for.
Sample code: buyers as 1x10 struct array with fields:
id,n,Budget
and the variable to find in the id values like id_test = 12
You can use the fact that if you have an array of structs, and you use the dot referencing, this creates a comma-separated list. If you enclose this in [] it will attempt to create an array and if you enclose it in {} it will be coerced into a cell array.
a(1).value = 1;
a(2).value = 2;
a(3).value = 3;
% Into an array
[a.value]
% 1 2 3
% Into a cell array
{a.value}
% [1] [2] [3]
So to do your comparison, you can convert the field you care about into either an array of cell array to do the comparison. This comparison will then yield a logical array which you can use to index into the original structure.
For example
% Some example data
s = struct('id', {1, 2, 3}, 'n', {'a', 'b', 'c'}, 'Budget', {100, 200, 300});
% Remove all entries with id == 2
s = s([s.id] ~= 2);
% Remove entries that have an id of 2 or 3
s = s(~ismember([s.id], [2 3]));
% Find ones with an `n` of 'a' (uses a cell array since it's strings)
s = s(ismember({s.id}, 'a'));

Matlab: convert int array into string array?

In Matlab I have integer array a=[1 2 3]. I need to convert them into one string, separated by ',':
c = '1,2,3'
If somehow I can have a string array b=['1' '2' '3'], then I can use
c = strjoin(b, ',')
to achieve the goal.
So my question is: How to convert integer array a=[1 2 3] into a string array b=['1' '2' '3']?
The int2str() is not working. It will give out
'1 2 3'
and it is not a "string array", so the strjoin can not apply to it to achieve '1,2,3'
You can simply use sprintf():
a = 1:3;
c = sprintf('%d,',a);
c = c(1:end-1);
There's a function in the file exchange called vec2str that'll do this.
You'll need to set the encloseFlag parameter to 0 to remove the square brackets. Example:
a = [1 2 3];
b = vec2str(a,[],[],0);
Inside b you'll have:
b =
'1,2,3'
I found one solution myself:
after getting the string (not array), split it:
b = int2str(); %b='1 2 3'
c = strsplit(b); %c='1' '2' '3'
Then I can get the result c=strjoin(c, ',') as I wanted.
You can use:
c = regexprep(num2str(a), '\s*', ',');

Resources