how to ask if an array is a string? - arrays

I'm trying to write a function to change arrays to hash values, I've done it so that I can change a numerical array into their hash values but I can't seem to figure out how to change string arrays into their hash values.
I'm having trouble doing it with string arrays. for example if array= [aced]
this is my code so far.
function [h]=Hash31(array1)
n=length(array1);
i=1;
h=0;
if array1
for i=1:1:n
h=array1(i)+31*h;
end
end
and it turns a numerical array into their hash value.
I was wondering if I had to do some kind of conversion or logical to make it work
and if I did to that how would I fit it into the existing function.
would it have to be a logical, because I've tried using an if function but I don't know how to write in code 'if the array is string, then...' in a way that Matlab understands.

Related

How to parse an array to a hash of arrays?

I'm a beginner (a wet lab biologist, who has to fiddle a bit with bioinformatics for the first time in my life) and today I've got stuck on one problem: how to parse an array to a hash of arrays in perl?
This doesn't work:
#myhash{$key} = #mytable;
I've finally circumvented my problem with a for loop:
for(my $i=0;$i<=$#mytable;$i++){$myhash{$key}[$i]=$mytable[$i]};
Of course it works and it does what I need to be done, but it seems to me not a solution to my problem, but just a way to circumvent it... When something doesn't work I like to understand why...
Thank you very much for your advice!
If you are asking how to put an array as one value of a hash, you do this by taking a reference to the array, since references are scalars and the values of hashes must be scalars. This is done with the backslash operator.
$myhash{$key} = \#mytable;
The for loop you describe creates such a reference through autovivification, as $myhash{$key}[0] creates an array reference at $myhash{$key} in order to assign to its index. Also note that the difference between taking a reference and copying each value is that in the former case, changes to the array after the fact will also affect the values referenced via the hash value, and vice versa.
$mytable[5] = 42; # $myhash{$key}[5] is also changed
As Grinnz mentioned you can save a reference to an array, but any change on the array latter will be reflected in hash (it is same data).
For example if you reuse same array in the loop then data in hash will reflect last iteration of the loop.
In such case you will want a copy of array stored in the hash.
#{$hash{$key}} = #array;
Programming Perl: Data strutures

How to sort algorithms with processing (beginner)?

Thanks for your time!
So I am trying to learn how to sort algorithms and watched a couple tutorial videos. Right now I am trying to declare an int variable "num" for values let's say 10-300. I am trying to create an int array "nums" that has "num" elements. Using random () to generate numbers from 1-1000 and put them in "nums". I wanted to implement 3 functions bubbleSort(), selectionSort(), and inserstionSort() that do bubble sort, selection sort, and insertion sort. After that I want to declare another int array "numt", copy all elements of nums array to numt and perform sorting in numt. After sorting completes, print the results with each number separated by a blank space. Hopefully this makes sense!
This question is a bit too broad for Stack Overflow. It's hard to answer general "how do I do this" type questions. Stack Overflow is more designed for specific "I tried X, expected Y, but got Z instead" type questions.
That being said, I'll try to help you in a general sense:
I am trying to learn how to sort algorithms
Small nitpick: you aren't sorting algorithms, you're using sorting algorithms to sort an array.
Right now I am trying to declare an int variable "num" for values let's say 10-300.
I'm not exactly sure why you need this variable, but you could do it pretty easily:
int num = 100; //assigns 100 to num
int num = int(random(10, 300)); //assigns random number between 10 and 300 to num
I am trying to create an int array "nums" that has "num" elements.
Just use the standard array syntax, passing in num as the size:
int[] nums = new int[num];
Using random () to generate numbers from 1-1000 and put them in "nums".
For this you'd use a for loop to assign each index of the array a random value.
After that I want to declare another int array "numt", copy all elements of nums array to numt and perform sorting in numt.
You can use another for loop to copy values from one array into another.
I wanted to implement 3 functions bubbleSort(), selectionSort(), and inserstionSort()
Start with something much simpler first, like a function that simply prints out the values in the array. From there you can implement more complicated logic.
do bubble sort, selection sort, and insertion sort
You can look these algorithms up on the internet. Wikipedia has entries on all of them.
But you might want to take a step back and ask yourself: how would you do this by yourself, without reading about any of these algorithms? If somebody handed you a bunch of index cards and asked you to put them in order, how would you do it? Try implementing that before you implement any other algorithms.
Also, you might want to ask yourself why you're doing this. Is it for homework? Or do you just need to sort an array? If so you might just use a built-in sorting function to do the work for you.

Access array elements from string argument in Modelica

I'm having a task in Modelica, where within a function, I want to read out values of a record (parameters) according to a given string type argument, similar to the dictionary type in Python.
For example I have a record containing coefficicents for different media, I want to read out the coefficients for methane, so my argument is the string "Methane".
Until now I solve this by presenting a second array in my coefficients-record storing the names of the media in strings. This array I parse in a for loop to match the requested media-name and then access the coefficients-array by using the found index.
This is obviously very complicated and leads to a lot of confusing code and nested for loops. Isn't there a more convenient way like the one Python presents with its dictionary type, where a string is directly linked to a value?
Thanks for the help!
There are several different alternatives you can use. I will add the pattern I like most:
model M
function index
input String[:] keys;
input String key;
output Integer i;
algorithm
i := Modelica.Math.BooleanVectors.firstTrueIndex({k == key for k in keys});
end index;
constant String[3] keys = {"A","B","C"};
Real[size(keys,1)] values = {1,2*time,3};
Real c = values[index(keys,"B")] "Coefficient";
annotation(uses(Modelica(version="3.2.1")));
end M;
The reason I like this code is because it can be made efficient by a Modelica compiler. You create a keys vector, and a corresponding data vector. The reason it is not a record is that you want the keys vector to be constant, and the values may vary over time (for a more generic dictionary than you wanted).
The compiler can then create a constant index for any constant names you want to lookup from this. This makes sorting and matching better in the compiler (since there are no unknown indexes). If there is a key you want to lookup at run-time, the code will work for this as well.

Retrieve multiple values from matlab string

I currently have a large single row array of chars... I also have two arrays, the first array has all the start indexes of data I would like to retrieve from the char array, the second array has all the end indexes for the data. How can I retrieve all these wanted values from my char array without using a loop?
So far I have tried doing
chararray(1,start(:):end(:))
but this will only retrieve the first value I would like!
Cheers!
Try this -
chararray(bsxfun(#plus,start1(:)-start1(1),start1(1):end1(1)))
This would create a 2D char array where each row be the output from each iteration of your loop code.
Also, please note that I am using start1 and end1 to represent your start and end arrays respectively, so as not to create a clash with the reserved terminate scope end used by MATLAB.

How to write a Matlab function that when given a single cell array of strings, returns a structure array with the same strings in alphabetical order?

I'm sure there may be a matlab function to do this but I'm required to write my own. As the title says, I need to write a function which when given a single cell array of strings, returns a structure array, containing the same strings but in alphabetical order. Furthermore, the 'count' fields must contain the number of times that that particular string has occurred eg
z=myfunction({'bag','dig','bag'})
ans =
str: 'bag'
count = 2
Ideally, the method should have an expected number of comparisons for n strings of O(n log n)
Assuming you don't want to use standard functions like sort or unique this is not an easy question. Furthermore it is more about math then about programming.
If you just want to practice programming, try implementing something simple like bubble sort.
This will not achieve O(n log n) however, if you really want that look into merge sort for example.
Several options are explained roughly here, and with a bit of searching it should not be hard to find what you need.

Resources