필터 지우기
필터 지우기

Changing elements of row after certain element

조회 수: 1 (최근 30일)
Rajesh
Rajesh 2022년 7월 14일
편집: Dyuman Joshi 2022년 7월 14일
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.

답변 (2개)

Dyuman Joshi
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]
x = 1×10
-1.0000 -0.6500 -0.4500 0 0.3000 0.8000 1.0000 0.4000 0.2000 -0.1000
%Code edited according to the question
x(find(x==1,1)+1:end)=0
x = 1×10
-1.0000 -0.6500 -0.4500 0 0.3000 0.8000 1.0000 0 0 0
  댓글 수: 3
Jan
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
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.

댓글을 달려면 로그인하십시오.


Jan
Jan 2022년 7월 14일
편집: Jan 2022년 7월 14일
x = [-1,-0.65,-0.45,0,0.3,0.8,1,0.4,0.2,-0.1];
idx = find(x == 1, 1);
if ~isempty(idx)
x(idx + 1:end) = 0;
end

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by