sorting two arrays simultaneously in Perl [duplicate] - perl

This question already has answers here:
How do you sort parallel arrays in Perl?
(4 answers)
How to return a sorted list's index in Perl?
(2 answers)
Closed 5 years ago.
I have two arrays.
1. #store_chromosomes( This is an array of arrays)
2. #fitness_values of the same length.
#store_chromosomes = ( ("attr1", "attr2", "attr3"), ("attr4", "attr5", "attr6"), ("attr_m", "attr_n", "attr_o")); #Just an example
#fitness_values=(-0.3456, -0.6789, 0.5678); #just an example
Elements in #fitness_values corresponding to elements in #store_chromosomes, i.e., -0.3456 is generated for ("attr1", "attr2", "attr3") attribute and so on.
I need to arrange the elements in #fitness_values in descending order, i.e., (-0.6789, -0.3456, 0.5678) at the same time I need to arrange the elements of #store_chromosomes corresponding to the order of #fitness_values, i.e., (("attr4", "attr5", "attr6"), ("attr1", "attr2", "attr3"), ("attr_m", "attr_n", "attr_o") ).
For #fitness_values, I can do it by #fitness_values = sort{4b<=>$a} #fitness_values. But how to do it simultaneously for the array #store_chromosomes.

Related

How to create an array by multiplying another array in a functional way? [duplicate]

This question already has answers here:
Repeating array in Swift
(4 answers)
Closed 5 years ago.
I'd like to create an array that contains elements from another array multiplied by some Int value.
Example:
the following code
let arr = [1,2,3]
let multiplier = 3
print(function(arr, multiplier))
should return
[1,2,3,1,2,3,1,2,3]
I know how to make it using nested for loops, but I'm looking for some nifty functional way. I was thinking about map() function, but it iterates over each element of a given array, which is not my use case I suppose.
Main idea:
Create array of arrays,
flatMap to one-dimensional array.
Example:
let arr = [1, 2, 3]
let multiplayer = 3
print(Array(repeating: arr, count: multiplayer).flatMap({ $0 }))

How to call a value from cell of array in matlab? [duplicate]

This question already has an answer here:
Access to a vector in cell in Matlab
(1 answer)
Closed 5 years ago.
i have this cell array in my Matlab code
energie{n} = [Ea, Ed];
In which Ea contain 1 value and Ed contain 3 values i don't know how to call the second value of Ed .
For example if i have this
Ea =
50.9982
Ed =
1.1777 19.0690 20.2442 8.5108
and i want to call '19.0690' how can i do it ?
I tried this
ans=energie{1:n:3}
but it give me
50.9982 1.1777 19.0690 20.2442 8.5108
Also i tried
energie{n}{2}(3)
But i got this error message
"Cell contents reference from a non-cell array object."
energie{n} is a 5 element matrix. You are wanting the 3rd element of it, so energie{n}(3) will give you the element.
If you had done energie{n} = {Ea Ed} then energie{n} is a cell array containing 2 elements, where the first element is a scalar and the second element is a 4 element vector. In this case energie{n}{2}(2) would work.

Get Distinct Members of an Array by property [duplicate]

This question already has answers here:
Removing Duplicates From Array of Custom Objects Swift
(4 answers)
Remove objects with duplicate properties from Swift array
(16 answers)
Closed 5 years ago.
I have an array
Contract object contains:
String:id
String:value
Array is:
contract1 = Contract.new()
contract1.id = 2
contract1.value = "Apple"
contract2 = Contract.new()
contract2.id = 2
contract2.value = "Pen"
contract3 = Contract.new()
contract3.id = 1
contract3.value = "Pineapple"
array = [Contract1, Contract2, Contract3]
I would to find out the list of contracts whose IDs are distinct.
I want to have a solution that doesn't make me change the implementation of my object (overriding the isEqual method etc) since I will be using it for more than one object through out my code.
Desired result:
[contract1, contract3] or [contract2, contract3]
Ideally, an extension with additionally a method to only return the values that are being made distinct:
Desired result: [2, 1]
I tried a couple of approaches from similar questions but either the answers are outdated or doesn't fit my need.

How to compare two vectors element by element? [duplicate]

This question already has answers here:
How do I compare all elements of two arrays?
(3 answers)
Closed 6 years ago.
I have two matrices: A=[1,2,3,4,5] and B=[1,2,3,4,6]. I need to compare elements of those matrix, and as a result I need to have binary matrix
Result=[1,1,1,1,0], that means if A(i)==B(i) then Result(i)=1 else Result(i)=0.
I have tried with:
if (isequal (A,B))
Result=1
else
Result=0
end
I have tried: Result=sign(A,B)
I hope that you could help me please?
A = [1,2,3,4,5]
B = [1,2,3,4,6]
Result = A == B

Remove specific integers from matrix [duplicate]

This question already has answers here:
Exclude elements from array [duplicate]
(3 answers)
Closed 6 years ago.
I have a matrix <1x1000> containing integers. It contain the value 150 a couple of times and I want to remove that value completely. Any ideas how to?
Help is much appreciated!
If you want to remove all elements that equal 150 then
M = M(M ~= 150)
If you want to remove all elements belonging to a list of undesired numbers then
list = [150, 230, 420]
M = M(~ismember(M, list))
Same but different expression
M(M==150)=[];
list = [150,230,420];
M(ismember(M,list))=[];
When you type A(index)=[], it delete A(index). For example,
A = [1,2,3];
A(2) = [];
Then
A = [1,3]