How to update global logical array from local logical array.

1 次查看(过去 30 天)
Hi, dear Community,
I want to create something like this:
i have global logical_array = [1,1,1,1,1,1,1,1]
I want to change the logical array to zero based on the sequence (the smallest value first)
from A: 1 (index 3) to be remove as first, then 2(index 5) as second, then 3(index 4) as third, then 4(index 1) as forth and 4 (index 5) as fifth,
from log_array = [1,1,0,1,1,1,1,1,], then [1,1,0,1,0,1,1,1], then [1,1,0,0,0,1,1,1], then [0,1,0,0,0,1,1,1], then [0,0,0,0,0,1,1,1],
At the same time i want to write the logical array into Zero, when that global position has been removed.
The end answer of logical array should be [0 0 0 0 0 1 1 1].
The loop should run until no values is < f_min.
How can i do this step by step?
Thanks a lot.
log_array = [true, true, true, true, true, true, true, true];
A = [4 4 1 3 2 7 8 9]';
DoF = 4;
f_min = 5;
r = length(A)-DoF;
while r ~= 0
if any (A< f_min)
[A_diff, f_id] = min(A-f_min);
log_array (f_id) = 0 %%Problem here
A (f_id) = []
r = length(A)-DoF; %r will be reduced by one in every loop
end
end

采纳的回答

Kartikay Sapra
Kartikay Sapra 2022-8-26
The issue with this approach is, whenever the size of the array is reduced, the indexing resets. Moreover, the global logical array does not change in size so it follows the initial indexing.
You may use the following logic:
log_array = [true, true, true, true, true, true, true, true];
A = [4 4 1 3 2 7 8 9]';
DoF = 4;
f_min = 5;
r = length(A)-DoF;
while r >= 0
if any (A< f_min)
[A_diff, f_id] = min(A-f_min);
log_array (f_id) = 0 %%Problem here
A (f_id) = intmax;
r = r-1; %r will be reduced by one in every loop
end
end
log_array = 1×8 logical array
1 1 0 1 1 1 1 1
log_array = 1×8 logical array
1 1 0 1 0 1 1 1
log_array = 1×8 logical array
1 1 0 0 0 1 1 1
log_array = 1×8 logical array
0 1 0 0 0 1 1 1
log_array = 1×8 logical array
0 0 0 0 0 1 1 1
A = A(log_array)
A = 3×1
7 8 9

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by