一つの列の中の任意の範囲内に正と負の値が両方がある場合に、その範囲内の値を0に置換するにはどうしたらよいですか。
조회 수: 9 (최근 30일)
이전 댓글 표시
一つの列の中の任意の範囲内に、正と負の値が両方ある場合にその範囲名の値を0に置換する方法を教えていただきたいです。
1,2,-3,4,5,6,7,8,9という要素があるとして、上記における任意の範囲を3としたときに、
0,0,0,4,5,6,7,8,9となるようなイメージです。
私はmatlabを使い始めたばかりで、このようなソフトウェアにもあまり触れてきていなかったため知識が乏しいです。
ご助力お願いいたします。
댓글 수: 1
Walter Roberson
2021년 10월 13일
Approximate translation:
I would like to know how to replace the value of the range name with 0 when there are both positive and negative values in an arbitrary range in one column.Assuming that there are elements 1,2,-3,4,5,6,7,8,9, when the arbitrary range in the above is set to 3,It is an image that becomes 0,0,0,4,5,6,7,8,9.I'm new to matlab and haven't had much knowledge of such software.Thank you for your help.
답변 (3개)
Hernia Baby
2021년 10월 13일
편집: Hernia Baby
2021년 10월 13일
質問の内容理解しましたので編集します
n = 4;
A = [1,2,-3,4,5,6,7,8,9];
負の値がある場合は1を返します
idx = A < 0
正と負がn=4の中に混在しているかを判定します
混在している場合は4番目までの数字は0になります
idx = idx(1:n)
if sum(idx)>=1 & sum(idx) < n
A(1:n) = 0;
end
A
댓글 수: 4
Hernia Baby
2021년 10월 14일
行ベクトルでなくて列ベクトル を扱っているからですね
見てみましょうか
w = 3;
A = [1,2,-3,4,5,6,-7,8,9,10]'
これが列ベクトルですね
この場合連結する方向が違います
num = length(A) - w + 1;
for i = 1:num
x = A(i:i+w-1);
if sum(x<0)*sum(x>=0)>0
x = zeros(w,1); %ここを列ベクトルにした
end
X{i} = x;
end
X
列ベクトルを並べているのがわかりますね
では連結していきましょう
B = X{1};
for i = 2:length(X)
x = X{i};
x1 = x(1:w-1);
x2 = x(end);
idx = B(end-w+2:end) == x1;
x1(idx) = 0;
x1 = [zeros(length(B)-w+1,1);x1]; %ここを列ベクトルにした
B = [B+x1;x2]; %ここを列ベクトルとして結合
end
B
snake
2021년 10월 13일
댓글 수: 1
Hernia Baby
2021년 10월 13일
自分のコメントに追加しました
以下は余談です
・全員に追記を展開する場合は質問部で編集すると周りも認知しやすくなります
・個人に対する返答ではコメントに記述すると、その人に通知が来るのでFBが来やすいです
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!