MATLAB: How to make specific elements of a matrix zero before nth element

conversion of elements to zero

Hi I have a matrix of 24×365. I have an array which reperesents the index of an element in each column.I want to convert all elements to zero in each column which are before that index value.
For example consider a 4×4 matrix.
A=[1 2 3 4
5 6 7 8
9 1 3 4
8 7 5 3]
B=[1,3,2,4] %Index array
so the new matrix should be c which is answer matrix.so the first element in B is the index for column 1.Its value is 1 and as there is no element before 1 so the whole column 1 remains the same in answer matrix C.Now for column 2,the index of the element is 3 so the first and second elemtents of column 2 will become zero in answer matrix C similarly for third column B(3)=2 so column 3 will have its first element as zero. Same condition applies for 4th column that before the 4th element in 4th column every element converts to zero.
Answer matrix:
C=[1 0 0 0
5 0 7 0
9 1 3 0
8 7 5 3]
How can I do that?

Best Answer

[m,n]=size(A);
C=A.*((1:m).'>=B);