Efficient Code for Filling Rows of an Array involving Indices Ranges
Hello,
I have an array A of zeros. I need to set a range of column indices for each row of A to ones. These ranges are determined by elements of another vector B, specifying start and end indices for the ranges for each row. For example, this is what I’m trying to figure out:
<</matlabcentral/answers/uploaded_files/18907/matrixA.jpg>>
<</matlabcentral/answers/uploaded_files/18908/matrixB.jpg>>
<</matlabcentral/answers/uploaded_files/18909/matrixA2.jpg>>
The first row of B indicates that the first row of A should have ones from columns 1 to 3. The second row of B indicates that the second row of A should have ones from columns 2 to 4 etc.
I could code this with a for-loop that goes through each of the rows and creates indices with the colon operator however, I’m hoping for a smarter, more efficient way to do it than that.
Can anyone help me with this or any suggestions?
Thanks.Hello,
I have an array A of zeros. I need to set a range of column indices for each row of A to ones. These ranges are determined by elements of another vector B, specifying start and end indices for the ranges for each row. For example, this is what I’m trying to figure out:
<</matlabcentral/answers/uploaded_files/18907/matrixA.jpg>>
<</matlabcentral/answers/uploaded_files/18908/matrixB.jpg>>
<</matlabcentral/answers/uploaded_files/18909/matrixA2.jpg>>
The first row of B indicates that the first row of A should have ones from columns 1 to 3. The second row of B indicates that the second row of A should have ones from columns 2 to 4 etc.
I could code this with a for-loop that goes through each of the rows and creates indices with the colon operator however, I’m hoping for a smarter, more efficient way to do it than that.
Can anyone help me with this or any suggestions?
Thanks. Hello,
I have an array A of zeros. I need to set a range of column indices for each row of A to ones. These ranges are determined by elements of another vector B, specifying start and end indices for the ranges for each row. For example, this is what I’m trying to figure out:
<</matlabcentral/answers/uploaded_files/18907/matrixA.jpg>>
<</matlabcentral/answers/uploaded_files/18908/matrixB.jpg>>
<</matlabcentral/answers/uploaded_files/18909/matrixA2.jpg>>
The first row of B indicates that the first row of A should have ones from columns 1 to 3. The second row of B indicates that the second row of A should have ones from columns 2 to 4 etc.
I could code this with a for-loop that goes through each of the rows and creates indices with the colon operator however, I’m hoping for a smarter, more efficient way to do it than that.
Can anyone help me with this or any suggestions?
Thanks. matrix manipulation MATLAB Answers — New Questions