# [Scilab-users] Problem with Matrix operation

4 messages
Open this post in threaded view
|
Report Content as Inappropriate

## [Scilab-users] Problem with Matrix operation

 Hello, I want to set certain elements of a matrix to 1. Interestingly the following code fills 9 elements with 1. I expected to have only 3 elements (with index 1,1; 2,2 and 3,3) filled with 1. A=zeros(5,3) inx=[1 2 3]; iny=[1 2 3]; A(inx,iny)=1 Is there any way to fill a matrix with 1 where I have the indexes in 2 vectors? Thanks for your help Wolfgang _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Problem with Matrix operation

 Hello, Le 25/04/2017 à 15:29, SCHULZ Wolfgang a écrit : > Hello, > I want to set certain elements of a matrix to 1. Interestingly the following code fills 9 elements with 1. I expected to have only 3 elements (with index 1,1; 2,2 and 3,3) filled with 1. > > A=zeros(5,3) > inx=[1 2 3]; > iny=[1 2 3]; > A(inx,iny)=1 Using this is for submatrix but what you want is not a submatrix. In your special case A=eyes(5,3) > Is there any way to fill a matrix with 1 where I have the indexes in 2 vectors? IMHO only the for loop works, in general... Jean-Yves _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Problem with Matrix operation

 In reply to this post by W.Schulz Hello, This should do the job if values different for a matrix containing only 0's and 1's: inx=[1 2 3]; iny=[1 2 3]; A=sparse([inx;iny]',ones(inx'),[5,3]); full(A) If non-indexed values are not known (and A already exists) then A(inx+size(A,'r')*(iny-1))=1; HTH Denis [@@ THALES GROUP INTERNAL @@] Unité Mixte de Physique CNRS / THALES 1 Avenue Augustin Fresnel 91767 Palaiseau CEDEx - France Tel : +33 (0)1 69 41 58 52 Fax : +33 (0)1 69 41 58 78 e-mail :  [hidden email]  http://www.trt.thalesgroup.com/ump-cnrs-thales http://www.research.thalesgroup.com-----Message d'origine----- De : users [mailto:[hidden email]] De la part de SCHULZ Wolfgang Envoyé : mardi 25 avril 2017 15:30 À : Users mailing list for Scilab Objet : [Scilab-users] Problem with Matrix operation Hello, I want to set certain elements of a matrix to 1. Interestingly the following code fills 9 elements with 1. I expected to have only 3 elements (with index 1,1; 2,2 and 3,3) filled with 1. A=zeros(5,3) inx=[1 2 3]; iny=[1 2 3]; A(inx,iny)=1 Is there any way to fill a matrix with 1 where I have the indexes in 2 vectors? Thanks for your help Wolfgang _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users Denis Crété
 In reply to this post by W.Schulz Le 25/04/2017 à 15:29, SCHULZ Wolfgang a écrit : ```Hello, I want to set certain elements of a matrix to 1. Interestingly the following code fills 9 elements with 1. I expected to have only 3 elements (with index 1,1; 2,2 and 3,3) filled with 1. A=zeros(5,3) inx=[1 2 3]; iny=[1 2 3]; A(inx,iny)=1 Is there any way to fill a matrix with 1 where I have the indexes in 2 vectors?``` ```A = zeros(5,3) inx = [1 2 3]; iny = [1 2 3]; A(sub2ind(size(A),inx,iny)) = 1 --> A(sub2ind(size(A),inx,iny)) = 1 A = 1. 0. 0. 0. 1. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. ``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users