How to update global logical array from local logical array.

3 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by