Matrix Indexing

2 次查看(过去 30 天)
Joerg
Joerg 2011-2-4
I have an index matrix.
idx = [1 5 ; 3 7 ]
The goal is to get a matrix like this:
1 0
0 0
0 1
0 0
1 0
0 0
0 1
Indexing the matrix like
a(idx) = 1;
doesn't work. Right now I use a loop, but I am not sure if this is the best way.

采纳的回答

Matt Fig
Matt Fig 2011-2-4
A = zeros(max(idx(:)),size(idx,1));
A(sub2ind(size(A),idx,cumsum(ones(size(idx))))) = 1

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by