How to update global logical array from local logical array.

조회 수: 2 (최근 30일)
Windel Hotdog
Windel Hotdog 2022년 8월 23일
답변: Kartikay Sapra 2022년 8월 26일
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