一つの列の中の任意の範囲内に正と負の値が両方がある場合に、その範囲内の値を0に置換するにはどうしたらよいですか。
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
一つの列の中の任意の範囲内に、正と負の値が両方ある場合にその範囲名の値を0に置換する方法を教えていただきたいです。
1,2,-3,4,5,6,7,8,9という要素があるとして、上記における任意の範囲を3としたときに、
0,0,0,4,5,6,7,8,9となるようなイメージです。
私はmatlabを使い始めたばかりで、このようなソフトウェアにもあまり触れてきていなかったため知識が乏しいです。
ご助力お願いいたします。
1 comentario
Walter Roberson
el 13 de Oct. de 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.
Respuestas (3)
Hernia Baby
el 13 de Oct. de 2021
Editada: Hernia Baby
el 13 de Oct. de 2021
質問の内容理解しましたので編集します
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 comentarios
Hernia Baby
el 14 de Oct. de 2021
行ベクトルでなくて列ベクトル を扱っているからですね
見てみましょうか
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
Walter Roberson
el 13 de Oct. de 2021
range = 3;
A = [1,2,-3,4,5,6,7,8,9]
A(A < range) = 0
3 comentarios
Hernia Baby
el 13 de Oct. de 2021
これは何をしてるかというと range という数字より下のものを 0 としているからです。
6と9は 4 以上なので 0 に置換されません。
snake
el 13 de Oct. de 2021
1 comentario
Hernia Baby
el 13 de Oct. de 2021
自分のコメントに追加しました
以下は余談です
・全員に追記を展開する場合は質問部で編集すると周りも認知しやすくなります
・個人に対する返答ではコメントに記述すると、その人に通知が来るのでFBが来やすいです
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!