Dynamic variable assignment based on vector element values in Octave - dynamic

I need to dynamically assign variable names to each row in a matrix based on the element values of the row so that I can later reference the appropriate row based on the variable rather than an index value.
I am having trouble identifying how to store the variable name along with its associated vector so that the vector can be later pulled up by simply typing in the variable name.
So far, using a simplified example I have:
A = [1 1 0; 1 0 1; 0 1 1];
s = cell(rows(A),1)
i = 1;
for i = 1:rows(A)
if (A(i,1) == 1 & A(i,2) == 1 & A(i,3) == 0) s(i,1) = struct("x1_y1_z0",{A(i,:)})
elseif (A(i,1) == 1 & A(i,2) == 0 & A(i,3) == 1) s(i,1) = struct("x1_y0_z1",{A(i,:)})
elseif (A(i,1) == 0 & A(i,2) == 1 & A(i,3) == 1) s(i,1) = struct("x0_y1_z1",{A(i,:)});
endif
i++
endfor
However, the resulting structue s lists x1_y1_z0 = 1 1 0 in a cell [1,1] and s.x1_y1_z0 does not return [1 1 0] as I would like.
Thanks very much for your guidance towards a working solution.

Related

Numpy.selectand assign new colum in df with condition from values of two column

df = df.assign[test = np.select[df.trs = 'iw' & df.rp == 'yu'],[1,0],'null']
I want if df.trs == iw' and df.rp == 'yu'` than new column should be created should be 0 else 1 only for condotion fullfilling row not every row
I tried no.slect and with condition array. But not getting desired output
You don't need numpy.select, a simple boolean operator is sufficient:
df['test'] = (df['trs'].eq('iw') & df['rp'].eq('yu')).astype(int)
If you really want to use numpy, this would require numpy.where:
df['test'] = np.where(df['trs'].eq('iw') & df['rp'].eq('yu'), 1, 0)

How to multiply two Sparse matrices by Spars method with python?

I tried to multiply the two Sparse matrices, but I had trouble deleting extra rows that were all zeros, I usednumpy.delete(my_matrix, [n], axis=0)and got this error:
index 4 is out of bounds for axis 0 with size 3
def mult_mat(mat1, mat2):
col = mat1[0][1]
row = mat2[0][0]
row_mat1, row_mat2 = np.shape(mat1)[0], np.shape(mat2)[0]
if col != row:
return "Multiplication is not possible because the number" \
" of columns in the first matrix is opposite of the" \
" number of rows in the second matrix"
my_matrix = np.array([[0] * 3] * (mat1[0][2] * mat2[0][2]))
n = 0
for r in range(1, row_mat1):
for h in range(1, row_mat2):
if mat1[r][1] == mat2[h][0]:
my_matrix[n][0], my_matrix[n][1], my_matrix[n][2] = mat1[r][0], mat2[h][1], mat1[r][2] * mat2[h][2]
n += 1
row_my_matrix = np.shape(my_matrix)[0]
for n in range(row_my_matrix):
if my_matrix[n][0] == 0 & my_matrix[n][1] == 0 & my_matrix[n][2] == 0:
my_matrix = np.delete(my_matrix, [n], axis=0)
return my_matrix

Unable to create Nested while-loop

I am supposed to "Write a program that uses two nested while loops to print off the rows and columns of a 3x3 grid (numbered 1 to 3), excluding the cells along the diagonal (i.e., where the row and column have the same value)."
I tried printing row, col = 1, 1 by adding one to each iteration.
row, col = 1, 1
while row != 3 and col != 3:
row += 1
col += 1
print (row, col)
Result should look like this:
1 2
1 3
2 1
vertically 12 on top, 13 in middle and 21 in the bottom.
A nested while loop generally means a second inside the first. I don't know what language you're using, but you should consider something more like:
while row <= 3:
while col <= 3:
print (row, col)
col++
col = 1
row++
This doesn't do the diagonal checks, but it demonstrates the nested loop idea:
The outer loop begins with row = 1 and col = 1.
The inner loop begins.
The inner loop counts columns 1-3, then exits;
The outer loop resets col to 1 and increments row
The next iteration of the outer loop begins with the new row.
Steps 2-5 repeat until until row = 4, at which point the outer loop exits and you're done.
Here's an example of this sort of thing in javascript, including the diagonal check:
let row = 1;
let col = 1;
// do this block until row > 3
while (row <= 3) {
// declare a new array to collect this row's output
let output = [];
// do this block until col > 3
while (col <= 3) {
// add the column or '-' to the end of the array
output.push( row === col ? '-' : col );
// increment the column
col++;
}
// row finished. emit the collected
// row with a space between the numbers
console.log(output.join(' '));
// reset column to 1
col = 1;
// do the next row
row++;
}

If Else Statement in vb.Net

I wanted to have this condition like i have b2.text, c3.text, d4.text, e5.text, f6.text, g7.text, h8.text, i9.text and j10.text.
And if they are all equal to zero then statement else continue.
i tried
If (b2.Text = 0 & c3.Text = 0 & d4.Text = 0 & e5.Text = 0 & f6.Text = 0 & g7.Text = 0 & h8.Text = 0 & i9.Text = 0 & j10.Text = 0) Then
a1.Text = 10000000
Else
Msgbox.Show("Cannot sort")
End if
Unfortunately i remembered & function only accept two variable only :P
How can i do it?
Thank you
You don't want & for VB, you want AndAlso. And while it is probably a bit above your level, you might want to look into Linq.
If ({b2.Text, c3.Text, d4.Text, e5.Text, f6.Text, g7.Text, h8.Text, i9.Text, j10.Text}).All(Function(f) f = "0") Then
a1.Text = 10000000
Else
Msgbox.Show("Cannot sort")
End IF
As others have said, you should turn on Option Strict, so that your comparison to an int gets flagged. I made the string "0" above, but you could easily modify it to use length if that is what you actually need.
I don't know what language are you using but I'm pretty sure you can compare more than 2 values The solution may be like this.
if (b2.text == 0 && c3.text == 0 && d4.text == 0 && e5.text== 0 && f6.text == 0 && g7.text == 0 && h8.text == 0 && i9.text == 0 && j10.text == 0) {
// when conditions are true
}
else {
// else code here
}

AutoIt - Split 1 variable into 4

I have one 4 digit variable i wish to split into 4 seperate variables fromt the range 0000 to 9999
Local $Data ="Element 1|Element 2|Element 3|Element 4"
Local $arr = StringSplit($Data, "|")
If IsArray($arr) Then
$Imax = Ubound($arr)
For $i = 0 to $Imax -1
Next
EndIf
This is what I got so far
I want it to do this:
Lets say the bigvar = 2345
$BigVar=2345 Then
$SmallVar1 = 2
$SmallVar2 = 3
$SmallVar3 = 4
$SmallVar4 = 5
Also the bigvar changes all the time so i need it to keep reading of that
LOL to all the overkill answers
#include <Array.au3>
Local $parts = StringSplit("1574", "")
_ArrayDisplay($parts)
String Approach
If you simply want to split it, you can go with
#include <Array.au3>
Func _Split($BigVar)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
Return $SmallVar
EndFunc
$Array = _Split("2345")
_ArrayDisplay($Array)
Now you can use
$Array[0] = 4 ;Amount of digits
$Array[1] = 2
$Array[2] = 3
$Array[3] = 4
$Array[4] = 5
If the Number might be 123 and you want to interpret it as 0123 therefore $SmallVar[1] being 0 not 1, this method might fit your needs:
#include <Array.au3>
Func _Split($BigVar, $Digits = 0)
Local $SmallVar[1] = [0]
For $i = 1 To StringLen($BigVar)
_ArrayAdd($SmallVar, StringMid($BigVar, $i, 1))
$SmallVar[0] += 1
Next
If $Digits = 0 Then Return $SmallVar
If $SmallVar[0] >= $Digits Then
For $i = 1 To $SmallVar[0] - $Digits
_ArrayDelete($SmallVar, $i)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndIf
For $i = 1 To $Digits - $SmallVar[0]
_ArrayInsert($SmallVar, 1, 0)
Next
$SmallVar[0] = $Digits
Return $SmallVar
EndFunc
$Array = _Split("123", 4) ;4 being the amount of digits
_ArrayDisplay($Array)
The code example above still works with this version, since digits is an optional parameter, and leaving it out, _Split will act as it did before.
Just use modulo 10 division to get the single integers.
#include <array.au3>
Global $BigVar=2345
Global $TmpVar=$BigVar
Global $aResult[StringLen(String($BigVar))]
For $i=UBound($aResult)-1 To 0 Step -1
$aResult[$i] = Int(Mod($TmpVar, 10))
$TmpVar /= 10
Next
_ArrayDisplay($aResult)
Now you got an array that has each number of the big integer stored in a separate field.