best way to update items in arrays value based on -1 and 1
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rizwan Khan
el 2 de En. de 2022
Comentada: Rizwan Khan
el 2 de En. de 2022
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];
0 comentarios
Respuesta aceptada
Image Analyst
el 2 de En. de 2022
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]
% 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.
Más respuestas (2)
Rik
el 2 de En. de 2022
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 comentarios
Rik
el 2 de En. de 2022
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])
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])
Walter Roberson
el 2 de En. de 2022
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.
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing 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!