Indexing and replacing value in a zero matrix based on a table
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to input a value in a zeros matrix of size 11*10. This is based on a variable which has three columns (Number, ID, ANS) as shown below. Now I want to insert the value in ANS which is -1 in the zeros matrix at 10x5 (ID x Number) position (index). Any suggestion on how this can be done?
Number ID ANS
5 10 -1
0 个评论
采纳的回答
Walter Roberson
2018-12-19
If your Number and ID and ANS are scalar then
YourMatrix(Number, ID) = ANS;
If they are vectors then there are multiple approaches, including:
YourMatrix( sub2ind(size(YourMatrix), Number(:), ID(:)) ) = ANS(:);
Or
YourMatrix = YourMatrix + accumarray( Number(:), ID(:), ANS(:), size(YourMatrix) );
and there are approaches using sparse.
0 个评论
更多回答(1 个)
Arvind Sathyanarayanan
2018-12-19
Is this what you're looking for?
ID = 10; Number = 5; Ans = -1;
tmat = zeros(11,10);
tmat(ID,Number)=Ans;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!