negative continuous position on vector

1 回表示 (過去 30 日間)
Alejandro Fernández
Alejandro Fernández 2020 年 8 月 16 日
コメント済み: hosein Javan 2020 年 8 月 17 日
Hi I have a question, i know how to solve with the code I show in the bottom of the page but i think that it has to be an easyer way. What I want to do is: starting with a vector row AA containing only one one in one position, for example:
AA = [0 0 1 0 0 0 0];
I want to get a BB vector that's next:
BB = [-2 -1 0 1 2 3 4];
A vector in which the numbers to the left of 1 are negative crescent in which the first component is the most negative position possible, the point 1 becomes 0 and the right of 1 is positive crescent until the end of the vector.
The code I know how to do is this:
AA = [0 0 1 0 0 0 0]
[m,n] = size(AA);
x = find(AA(1,:),1)
neg = AA(1:x-1)
BB(1,x) = 0
BB(1,1:length(neg)) = -length(neg):1:-1
BB(1,x+1:n) = 1:1:n-x
But I don't see that this is a very good way to get the solution...

採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 16 日
編集済み: Bruno Luong 2020 年 8 月 17 日
BB = (1:length(AA))-find(AA==1,1)
  1 件のコメント
Alejandro Fernández
Alejandro Fernández 2020 年 8 月 17 日
Well, you could make it a lot shorter than I did... thank you very much.

サインインしてコメントする。

その他の回答 (2 件)

hosein Javan
hosein Javan 2020 年 8 月 16 日
how about:
k = find(logical(A));
n = length(A);
B = 1-k:n-k
A =
0 0 0 0 1 0 0 0
B =
-4 -3 -2 -1 0 1 2 3
  2 件のコメント
Alejandro Fernández
Alejandro Fernández 2020 年 8 月 17 日
Yes thank you very much, I have to give the MVP to Bruno because he did it earlier and in less steps but I appreciate it very much.
hosein Javan
hosein Javan 2020 年 8 月 17 日
Ofcourse. thank you.

サインインしてコメントする。


Sara Boznik
Sara Boznik 2020 年 8 月 16 日
AA=[0 0 1 0 0 0 0]
ne=-1;
po=1;
[n,m]=size(AA)
b=find(AA(1,:)==1)
BB=zeros(n,m)
for i=1:b-1
BB(1,i)=ne
ne=ne-1
end
for j=b+1:m
BB(1,j)=po
po=po+1
end
  1 件のコメント
Alejandro Fernández
Alejandro Fernández 2020 年 8 月 17 日
Thank you so much, you can see the previous coments, they make it with just 1 step, thank you so much by the way.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by