一つの列の中の任意の​範囲内に正と負の値が​両方がある場合に、そ​の範囲内の値を0に置​換するにはどうしたら​よいですか。

9 views (last 30 days)
snake
snake on 13 Oct 2021
Commented: snake on 14 Oct 2021
一つの列の中の任意の範囲内に、正と負の値が両方ある場合にその範囲名の値を0に置換する方法を教えていただきたいです。
1,2,-3,4,5,6,7,8,9という要素があるとして、上記における任意の範囲を3としたときに、
0,0,0,4,5,6,7,8,9となるようなイメージです。
私はmatlabを使い始めたばかりで、このようなソフトウェアにもあまり触れてきていなかったため知識が乏しいです。
ご助力お願いいたします。
  1 Comment
Walter Roberson
Walter Roberson on 13 Oct 2021
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.

Sign in to comment.

Answers (3)

Hernia Baby
Hernia Baby on 13 Oct 2021
Edited: Hernia Baby on 13 Oct 2021
質問の内容理解しましたので編集します
n = 4;
A = [1,2,-3,4,5,6,7,8,9];
負の値がある場合は1を返します
idx = A < 0
idx = 1×9 logical array
0 0 1 0 0 0 0 0 0
正と負がn=4の中に混在しているかを判定します
混在している場合は4番目までの数字は0になります
idx = idx(1:n)
idx = 1×4 logical array
0 0 1 0
if sum(idx)>=1 & sum(idx) < n
A(1:n) = 0;
end
A
A = 1×9
0 0 0 0 5 6 7 8 9
  4 Comments
Hernia Baby
Hernia Baby on 14 Oct 2021
行ベクトルでなくて列ベクトル を扱っているからですね
見てみましょうか
w = 3;
A = [1,2,-3,4,5,6,-7,8,9,10]'
A = 10×1
1 2 -3 4 5 6 -7 8 9 10
これが列ベクトルですね
この場合連結する方向が違います
horzcatvertcat を参照ください
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
X = 1×8 cell array
{3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double}
列ベクトルを並べているのがわかりますね
では連結していきましょう
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
B = 10×1
0 0 0 4 5 6 0 8 9 10
snake
snake on 14 Oct 2021
そういうことだったのですね。
行ベクトルと列ベクトル、しっかりと覚えました。
様々なことを丁寧に教えていただき、本当にありがとうございました。
自分なりに調べたり試行錯誤しても分からないことだらけで、学校の先生に聞いても自分でなんとかしての一点張りで、心が折れそうなところを助けていただきました。
改めて、このたびは本当にありがとうございました。
失礼いたします。

Sign in to comment.


Walter Roberson
Walter Roberson on 13 Oct 2021
range = 3;
A = [1,2,-3,4,5,6,7,8,9]
A = 1×9
1 2 -3 4 5 6 7 8 9
A(A < range) = 0
A = 1×9
0 0 0 4 5 6 7 8 9
  3 Comments
Hernia Baby
Hernia Baby on 13 Oct 2021
これは何をしてるかというと range という数字より下のものを 0 としているからです。
6と9は 4 以上なので 0 に置換されません。
snake
snake on 13 Oct 2021
そうだったのですね。
コメントありがとうございます。

Sign in to comment.


snake
snake on 13 Oct 2021
わかりやすい回答ありがとうございます。
「範囲内に~」の範囲をずらしていくことは可能なのでしょうか。
文章での説明が難しいため、図を添付しますのでそちらを見ていただけると有難いです。
また、これらの数値は小数でも実行することはできるでしょうか。
見当違いなことを言っていましたら申し訳ありません。
何卒宜しくお願い致します。
  1 Comment
Hernia Baby
Hernia Baby on 13 Oct 2021
自分のコメントに追加しました
以下は余談です
・全員に追記を展開する場合は質問部で編集すると周りも認知しやすくなります
・個人に対する返答ではコメントに記述すると、その人に通知が来るのでFBが来やすいです

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!