Combine two arrays in Bash line by line [duplicate] - arrays

This question already has answers here:
How to merge two arrays in a zipper like fashion in Bash?
(6 answers)
Closed 6 years ago.
I have 2 arrays in BASH and I want to combine them line by line i.e.
arr1=( 1 2 3 4 )
arr2=( 5 6 7 8 )
When simply adding one array to another it's like 1 2 3 4 5 6 7 8 and I want the output of this combination to be 1 5 2 6 3 7 4 8 (line by line)
Any advice?

arr1=( 1 2 3 4 )
arr2=( 5 6 7 8 )
declare -a result
resultIndex=0
for index in ${!arr1[*]}; do
result[$resultIndex]=${arr1[$index]}
let "resultIndex++"
result[$resultIndex]=${arr2[$index]}
let "resultIndex++"
done
echo "${result[#]}"

Related

How to use a nested for loop to create a 1D array of integer pairs on a range?

In bash, I am trying to create 1-D array that contains all possible integer pairs on a range from a low value to a high value (i.e. 1 to 2)
I've tried using a nested for loop, however the out put I get is the array of the correct size, but all values are the high value (in this case 2)
I've tried nested for loops, however the array I am creating is not the correct size nor does it contain the correct combinations.
for (( i=$low; i<=$high; i++ ))
do
range_array[i]=$i
done
range=${#range_array[#]}
range_squared=$(( $range*$range))
new_range=$(( 2*$range_squared))
for (( i = $low; i <= $high; i++ ));do
for (( j = 1; j <= $new_range; j++ )) do
combo_array[j]=$i
done
done
echo "the following is the combo array"
echo ${combo_array[#]}
I expect the combo_array to be:
1 1 1 2 2 1 2 2
instead it is
2 2 2 2 2 2 2 2
That's too much work for such a trivial task. Here is a simple and working one:
combo_array=()
for ((i=low; i<=high; ++i)); do
for ((j=low; j<=high; ++j)); do
combo_array+=("$i" "$j")
done
done
echo "${combo_array[#]}"
Given low=6 and high=9, it outputs
6 6 6 7 6 8 6 9 7 6 7 7 7 8 7 9 8 6 8 7 8 8 8 9 9 6 9 7 9 8 9 9

Using multiple indicies to grab values from 2D MATLAB array without returning all combinations [duplicate]

This question already has an answer here:
3d matrix: how to use (row, column) pairs with 3rd dimension wildcard in MATLAB?
(1 answer)
Closed 5 years ago.
I am trying to get a few values from a 2D matrix
Consider the starting matrix:
>> test = randi(10,10)
test =
10 4 8 7 10 4 2 8 4 1
6 5 6 5 5 2 10 4 7 6
7 2 5 4 1 1 3 7 6 1
1 3 3 9 9 5 10 4 6 9
9 1 8 4 7 2 3 7 3 10
8 10 3 9 4 8 4 1 3 1
2 7 1 8 10 4 1 10 5 10
6 10 8 9 3 9 7 9 3 1
4 2 7 6 7 8 2 8 9 7
6 10 8 7 7 6 1 9 10 8
What I want to do is grab elements (1,4);(2,5);and(3,6) only
So I try
test([1,2,3],[4,5,6])
but that returns all combinations of the two indicies!
ans =
6 3 1
1 2 4
8 4 8
Without this intermediate step, how do I do what I want in one line? There must be a way.
I cannot use the intermediate step because in actuality, my matrix is very large and so are my indices lengths so I will run out of memory.
You can do this using sub2ind as was already pointed out at mathworks:
test(sub2ind(size(test),[1,2,3],[4,5,6]))
Applied to 3D
test = randi(10,10,10,10);
test(sub2ind(size(test),[1,2,3],[4,5,6], [3,3,3]))

Build a large array from a small array in MATLAB? [duplicate]

This question already has answers here:
Octave / Matlab: Extend a vector making it repeat itself?
(3 answers)
Closed 8 years ago.
If I have a small array like a=[1 2 3 4 5], and want to build a large array from it with repeating it, like b=[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ....1 2 3 4 5], how can I do that in simplest way and lowest calculations?
repmat is what you are looking for
n = 5
b = repmat(a,1,n)

Matlab- Combinations for subsets of a set

Hi may I know how i can perform this using matlab?
I tried nchoosek but only works on 1 type of combination. I would like to output all together in array
Let S={a,b,c,d,e}
I would like to get combinations as such which start from 3 combinations:
the 3-combinations : {a,b,c} , {a,b,d} , {a,b,e} , {a,c,d} , {a,c,e} , {a,d,e}
the 4-combinations : {a,b,c,d} , {a,b,c,e} , {a,c,d,e}
the 5-combinations : {a,b,c,d,e}
So the output would be like this:
{a,b,c} {a,b,d} {a,b,e} {a,c,d} {a,c,e} {a,d,e}{a,b,c,d} {a,b,c,e} {a,c,d,e}{a,b,c,d,e}
Thanks
You can use a loop there or arrayfun which is just a compact way to express such a loopy approach and not a vectorized approach -
combs = arrayfun(#(x) nchoosek(S,x),3:numel(S),'Uniform',0)
The output would be a cell array with each cell representing values for each combination. So, when you run the code, you would get -
>> combs{1}
ans =
2 7 4
2 7 1
2 7 9
2 4 1
2 4 9
2 1 9
7 4 1
7 4 9
7 1 9
4 1 9
which would be your 3-combinations set.
>> combs{2}
ans =
2 7 4 1
2 7 4 9
2 7 1 9
2 4 1 9
7 4 1 9
would be your 4-combinations set and so on.

How do I split a text file into blocks using Perl?

I have a data file with lines like this:
A1 2 3 4 5
B 1 2 4
B 7 8 9
A6 7 8 9
B 1 2 3
B 5 6 7
A3 6 9 7
B 2 3 3
B 5 6 6
Using Perl, how do I split the file into a set of arrays (or any other data structure) when the parser hits a /^A/ please?
so I end up with
array1:
A1 2 3 4 5
B 1 2 4
B 7 8 9
array2:
A6 7 8 9
B 1 2 3
B 5 6 7
etc.
Many thanks.
I had to rewrite the answer (after rewritten question)
#arrays = ();
while (<>) {
push(#arrays, []) if /^A/;
push(#{$arrays[-1]}, $_)
}
It is at times like this when I wish $/ could be more than just a string. Nevertheless, there are workarounds.
One could slurp the file in and process with a lookahead assertion. The example below simply prints each string delimited with << >>, but the basic idea is the same regardless of what you want to do with the data:
$ perl -0777 -wE 'say "<<$_>>" for split /(?=^A)/m, <>' file.txt
<<A1 2 3 4 5
B 1 2 4
B 7 8 9
>>
<<A6 7 8 9
B 1 2 3
B 5 6 7
>>
<<A3 6 9 7
B 2 3 3
B 5 6 6
>>

Resources