I would like to use a “match with” function to compare two matrix.
I think I don’t understand something about this function.
Let’s take a trivial example. Consider 2 simple matrix:
let a = matrix [[1.0; 1155.0; 1.0]
[1.0; 1156.0; 1.0]
[2.0; 1157.0; 1.0]
[3.0; 1157.0; 1.0]]
let b = matrix [[1.; 0.; 1.0]
[1.0; 0.; 1.0]
[2.0; 0.; 1.0]
[3.0; 0.; 1.0]]
Now I would like to copy the second column of matrix “a” in matrix “b”. But with the following code, I have the error which say “error FS0010: unexpected symbole ‘[‘ in the expression. use ‘(‘, ‘()’ …”*.
Here is the code :
for i = 0 to a.NumCols -1 do
match b.[i,0] with
| a.[i,0] -> (b.[i,1]<-a.[i,1])
| _ -> (b.[i,1]<-a.[i,1])
The problem seems to come from a.[i,0] which is not recognized or unexpected. I don’t understand why as I just ask to match a float (b.[i,0]) with another float (a.[i,0]).
Can you give me an explanation?
I don’t think the
matchkeyword is useful in this case. That is only useful if you need to do different things depending on a value of some expression. You can solve the problem easily by writing:This performs a bulk operation – it takes a slice that represents the entire second column of
aand assigns it to a slice that represents the entire second column ofb.If you wanted to write this using
forloop, you could use:For more information about using the
matrixtype, you can read my recent blog post. It usesmatrixto represent graphs, but it explains all the basic syntax and operations you can do with matrices.EDIT To answer the additional question – if you want to check whether the matched value is the same as some other value (such as element in another matrix), then you need to use the
whenclause: