Changing elements of row after certain element
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a m*n matrix where some of rows are like these for exmaple
[-1,-0.65,-0.45,0,0.3,0.8,1,0.4,0.2,-0.1]
Now I need to make new row as follows
[-1,-0.65,-0.45,0,0.3,0.8,1,0,0,0]
that is once any of the elements in row becomes 1 after that all the elements will be zero.
0 个评论
回答(2 个)
Dyuman Joshi
2022-7-14
编辑:Dyuman Joshi
2022-7-14
For a matrix run a loop through the rows
x=[-1,-0.65,-0.45,0,0.3,0.8,1,0.4,0.2,-0.1]
%Code edited according to the question
x(find(x==1,1)+1:end)=0
3 个评论
Jan
2022-7-14
This deletes all elements after a 1. The OP asked for setting the values to 0.
find() can reply an empty matrix or a vector. This works, but only, because the colon operator ignores all elements of the vector except for the 1st one. This is at least confusing.
Dyuman Joshi
2022-7-14
You are correct, Jan, my code doesn't do what OP asked. The code written is according to what I had in mind at the time.
I will edit my code accordingly.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!