F# 与 for 矩阵匹配

发布于 2024-12-16 17:07:00 字数 727 浏览 0 评论 0原文

我想使用“匹配”函数来比较两个矩阵。 我想我对这个功能有些不明白。 让我们举一个简单的例子。考虑2个简单矩阵:

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]]

现在我想将矩阵“a”的第二列复制到矩阵“b”中。但使用下面的代码时,出现错误,显示“错误 FS0010:表达式中出现意外符号 '['。使用 '(', '()' ...”*。 这是代码:

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])

问题似乎来自 a.[i,0] ,它无法识别或意外。我不明白为什么,因为我只是要求将一个浮点数 (b.[i,0]) 与另一个浮点数 (a.[i,0]) 匹配。 你能给我一个解释吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-12-23 17:07:00

我认为 match 关键字在这种情况下没有用。仅当您需要根据某些表达式的值执行不同的操作时,这才有用。您可以通过编写轻松解决问题:

b.[0 .. b.NumCols, 1 .. 1] <- a.[0 .. b.NumCols, 1 .. 1] 

这将执行批量操作 - 它需要一个表示 a 的整个第二列的 slice 并将其分配给表示b 的整个第二列。

如果您想使用 for 循环来编写此代码,您可以使用:

for i in 0 .. b.NumCols - 1 do 
  b.[i, 1] <- a.[i, 1]

有关使用 matrix 类型的更多信息,您可以阅读 我最近的博客文章。它使用矩阵来表示图形,但它解释了可以对矩阵执行的所有基本语法和操作。

编辑 要回答附加问题 - 如果您想检查匹配的值是否与其他值(例如另一个矩阵中的元素)相同,那么您需要使用 when< /代码> 子句:

match a.[0,0] with
| v when v = b.[0,0] -> () // Do nothing, they are the same
| v -> a.[0,0] <- v        // Set a.[0,0] to b.[0,0] because they are different

I don't think the match keyword 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:

b.[0 .. b.NumCols, 1 .. 1] <- a.[0 .. b.NumCols, 1 .. 1] 

This performs a bulk operation - it takes a slice that represents the entire second column of a and assigns it to a slice that represents the entire second column of b.

If you wanted to write this using for loop, you could use:

for i in 0 .. b.NumCols - 1 do 
  b.[i, 1] <- a.[i, 1]

For more information about using the matrix type, you can read my recent blog post. It uses matrix to 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 when clause:

match a.[0,0] with
| v when v = b.[0,0] -> () // Do nothing, they are the same
| v -> a.[0,0] <- v        // Set a.[0,0] to b.[0,0] because they are different
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文