How to return value to zero before index of specific element in matrix?

3 views (last 30 days)
Hi all!
I have a matrix (m*n), in each row i have a specific element that i want all element before it to be set to zero.
I extracted the index of specific element in column vector "V" . all i need now is to return values before this index "0".
thanks

Accepted Answer

Matt J
Matt J on 12 Sep 2022
Edited: Matt J on 12 Sep 2022
m=4;n=8;
Matrix=rand(m,n),
Matrix = 4×8
0.1866 0.9389 0.4492 0.7203 0.0563 0.4231 0.2116 0.7976 0.8085 0.5572 0.6158 0.9831 0.7248 0.2271 0.2121 0.2003 0.1690 0.7889 0.3033 0.0181 0.7499 0.4327 0.9734 0.0099 0.5639 0.1376 0.0910 0.0868 0.3564 0.9456 0.1187 0.6761
V=[3;7;2;5];
Matrix=Matrix.*((1:n)>=V),
Matrix = 4×8
0 0 0.4492 0.7203 0.0563 0.4231 0.2116 0.7976 0 0 0 0 0 0 0.2121 0.2003 0 0.7889 0.3033 0.0181 0.7499 0.4327 0.9734 0.0099 0 0 0 0 0.3564 0.9456 0.1187 0.6761

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!