如何根据行和列号创建数组 [英] How to create an array according to row and column number
本文介绍了如何根据行和列号创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想根据行号和列号创建一个数组.
I want to create an array according to row number and column number.
例如:
row_number Column_number Value
1 1 5
3 2 10
4 6 4
7 5 66
该数组应类似于:
A=
5 0 0 0 0 0
0 0 0 0 0 0
0 10 0 0 0 0
0 0 0 0 0 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 66 0
否则它将打印零.
推荐答案
此常见问题的3种可能方法:
3 possible methods to this regularly asked question:
1.使用 sub2ind
函数
A = zeros(max(row_number), max(Column_number));
idx = sub2ind(size(A),row_number, Column_number);
A(idx) = Value;
2.手动计算线性索引
2. Calculating the linear indices manually
A = zeros(max(row_number), max(Column_number));
idx = row_number(:,1) + (Column_number(:,2)-1)*size(A,1)
A(idx) = Value;
3.使用 sparse
矩阵
sparse(row_number, Column_number, Value)
然后调用 full
如果要将其转换为常规矩阵
And then call full
on that if you want to convert it to a regular matrix
这篇关于如何根据行和列号创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文