Printable Version of Topic

Click here to view this topic in its original format

Linuxhelp _ Programming in Linux _ Loop over Reducing Matrix Dimensionality

Posted by: Valjean Feb 29 2016, 05:57 PM

My goal is to take a set of vectors, which run over an index i, and reduce their dimensionality by one for every zero in the array. In particular, I have a set of vectors which have elements

vecA_$i=[ a 0 0 0 0 0 0 0 0 0 0 0 0 0 0 b ]

and I just want to reindex them to have the vector [ a b ]. If I have just one vector, I can do

VecA_1=("${VecA_1[@]:0}")

and this works just fine. However, I am having difficulty doing this over a loop. I attempted to do

for (( i=1; i<=$directory; i++ ))
do
VecA_$i=("${VecA_$i[@]:0}")
done

but this results in a syntax error on the definition line. Can anyone advise how to perform a do loop over a matrix dimensionality reduction?

Posted by: michaelk Mar 1 2016, 12:50 PM

You have to use indirect like before:

CODE
i=1
test_1=(a 1 2 3 4 5 6 b)
echo "before" ${test_1[@]}
dtest=temp_$i
eval test_${i}="${!dtest[@]:2}"
echo "after" ${test_1[@]}


The string command ${string:0] extracts the entire string so basically nothing happens. The basic command extracts the substring starting at position x. In my code x=2 so the array would then be:
(1 2 3 4 5 6 cool.gif. I don't know if it will work but you can try to use use regular expressions (expr) to extract all zeros.

www.tldp.org/LDP/abs/html/string-manipulation.html

Posted by: Valjean Mar 2 2016, 12:59 AM

QUOTE (michaelk @ Mar 1 2016, 12:50 PM) *
You have to use indirect like before:
CODE
i=1
test_1=(a 1 2 3 4 5 6 b)
echo "before" ${test_1[@]}
dtest=temp_$i
eval test_${i}="${!dtest[@]:2}"
echo "after" ${test_1[@]}


The string command ${string:0] extracts the entire string so basically nothing happens. The basic command extracts the substring starting at position x. In my code x=2 so the array would then be:
(1 2 3 4 5 6 cool.gif. I don't know if it will work but you can try to use use regular expressions (expr) to extract all zeros.

www.tldp.org/LDP/abs/html/string-manipulation.html


Thank you!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)