Indexing and replacing value in a zero matrix based on a table
3 visualizaciones (últimos 30 días)
Sunny el 19 de Dic. de 2018
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
Walter Roberson el 19 de Dic. de 2018
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(:);
YourMatrix = YourMatrix + accumarray( Number(:), ID(:), ANS(:), size(YourMatrix) );
and there are approaches using sparse.