objective-c - 如何在 Objective c 中转置矩阵?

标签 objective-c matrix

我有一个二维矩阵。我怎样才能将它转置到 objective-c 中?

我是否使用嵌套循环来转置它,因为我在数组和可变数组类中找不到任何方法。

我的矩阵是 3x3。

NSMutableArray *row1=[NSMutableArray arrayWithObjects:@1,@2,@3, nil];
NSMutableArray *row2=[NSMutableArray arrayWithObjects:@4,@5,@6, nil];
NSMutableArray *row3=[NSMutableArray arrayWithObjects:@7,@8,@9, nil];

NSMutableArray *twoDArray=[NSMutableArray arrayWithObjects:row1,row2,row3, nil];
NSLog(@"%@",twoDArray);

之后怎么办?

最佳答案

就是这么简单:

NSMutableArray *result = [NSMutableArray arrayWithCapacity:3];

for (NSUInteger i = 0; i < 3; i++) {
    [result addObject:@[row1[i], row2[i], row3[i]]];
}

为简单起见,我假设它始终是 3x3 矩阵。

关于objective-c - 如何在 Objective c 中转置矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14091740/

相关文章:

ios - ReactiveCocoa replayLast : + take:1

objective-c - Objective C 中 AudioToolbox 的引用错误

r - 错误: requires numeric/complex matrix/vector arguments when using matrix times vector multiplication

c++ - 如何使用 Rcpp 库将 C++ 中包含 double 值的 vector vector 转换为 R 中的矩阵?

objective-c - Objective - C 静态库和它的公共(public)头文件 - 什么是正确的方法?

ios - UIViewPropertyAnimation Objective C 问题

matlab - 具有大量零的矩阵的高效乘法

iPhone:如何使用CGContextConcatCTM正确保存转换后的图像?

objective-c - 什么时候应该在 Objective C 类上使用前缀?

C++ 重载 : operator+ for adding Matrices by element