best way to update items in arrays value based on -1 and 1

2 ビュー (過去 30 日間)
Rizwan Khan
Rizwan Khan 2022 年 1 月 2 日
コメント済み: Rizwan Khan 2022 年 1 月 2 日
I have the following arrays
The first arr (ArrA), that is the input
The second arr (ArrB) that is the desired results.
Now the logic is that when it see's a 1, the value continues to be a 1, until some new (nonzero) number is found.
When it then sees the new number (eg -1), the following values are then -1, until a new (nonzero) value is found.
So basically it start with 0, 0, 0, it then see's a 1, so the value is 1, until it finds a -1, when it finds that the values are -1 until it finds a 1, and so on.
Numbers int his array will always be either 0, 1, -1.
Now i've been thinking of loops and stuff, but it all seems very complicated with loops within loops
so any help will be appreciated.
ArrA = [0,0,0,1,0,1,0,-1, 0, 0,-1, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0, 0];
ArrB = [0,0,0,1,1,1,1,-1,-1,-1,-1,-1 -1, 1, 1, 1, 1, 1, 1, -1, -1, -1];

採用された回答

Image Analyst
Image Analyst 2022 年 1 月 2 日
This will do it, if you have the Image Processing Toolbox.
ArrA = [0,0,0,1,0,1,0,-1, 0, 0,-1, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0, 0];
ArrBRef = [0,0,0,1,1,1,1,-1,-1,-1,-1,-1 -1, 1, 1, 1, 1, 1, 1, -1, -1, -1]
ArrBRef = 1×22
0 0 0 1 1 1 1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 -1 -1 -1
% Initialize output array
ArrB = ArrA;
props = regionprops(ArrA == 0, 'PixelList'); % Get indexes of each group of zeros.
% Loop over each group, reassigning zeros to the value prior to the zeros.
for k = 1 : length(props)
theseIndexes = props(k).PixelList(:, 1); % Get indexes for this group only.
% Skip the set of 0's if it's at the beginning because there is no prior value.
if theseIndexes(1) == 1
continue; % Skip to bottom of loop.
end
% Set values at these indexes equal to the prior value.
ArrB(theseIndexes) = ArrB(theseIndexes(1) - 1);
end
ArrB % Display in command window.
ArrB = 1×22
0 0 0 1 1 1 1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 -1 -1 -1
  1 件のコメント
Rizwan Khan
Rizwan Khan 2022 年 1 月 2 日
Thank you Sir,
This solves my problem

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

その他の回答 (2 件)

Rik
Rik 2022 年 1 月 2 日
Use find to find out how many leading zeros you have. Then set all zeros to 1 and use cumprod. Then set the leading zeros as required.
There is probably a more efficient way to do this, but this will get you most of the way there.
  2 件のコメント
Rizwan Khan
Rizwan Khan 2022 年 1 月 2 日
Thanks for your help, however, setting all zeros to one 1 does not solve the problem.
Some of the zeros will become a 1, while others need to be -1.
So i'm unsure how your suggested approach will resolve it for me.
Rik
Rik 2022 年 1 月 2 日
That is what the cumprod is for.
% if this is your array:
% [1 0 -1 0]
% then this will be the result of the cumprod call
cumprod([1 1 -1 1])
ans = 1×4
1 1 -1 -1
Although I now realise that this will not work if you have multiple -1 in a stretch:
% [1 0 -1 0 -1]
cumprod([1 1 -1 1 -1])
ans = 1×5
1 1 -1 -1 1

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


Walter Roberson
Walter Roberson 2022 年 1 月 2 日
You do not need loops within loops.
Keep a single variable that records what you are currently converting 0 to.
Scan. At each position, if the input is 1, output is 1, record that you are converting to 1. If the input is -1, output is -1, record that you are converting to -1. Otherwise, input must be 0, and output the recorded value.
WIth some work you can get it down to a single comparison for each input character.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by