overlap logical matrices in MATLAB

1 回表示 (過去 30 日間)
Ban
Ban 2023 年 10 月 4 日
コメント済み: Star Strider 2023 年 10 月 17 日
I have code which runs multiple times (say, 3 times). The code produces logical matrix denoted by 'a' in each loop (denoted by n). I want to get an overlapped logical matrix at the end of the loop. Please suggest.
clear;
close all;
clc;
dis_threshold=0.4;
for n=1:3
xa_time_step=rand(1,5);
za_time_step=rand(1,5);
for k1=1:length(xa_time_step)
for j1=1:length(xa_time_step)
d(j1,k1)=sqrt((xa_time_step(j1)-xa_time_step(k1)).^2+(za_time_step(j1)-za_time_step(k1)).^2);
end
end
d1=abs(d);
d1(d1==tril(d1)) = NaN ;
a=d1 < dis_threshold ;
disp(a)
end
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 4 日
"I want to get an overlapped logical matrix at the end of the loop."
What does overlap mean in this context? Take Logical OR of the outputs? Logical AND? Concatenate the arrays? Horizontally or vertically? or something else?
Fabio Freschi
Fabio Freschi 2023 年 10 月 4 日
What do you mean with "overlapped logical matrixp"? is it the & operator of the 3 matrices created in the loop?

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

採用された回答

Star Strider
Star Strider 2023 年 10 月 4 日
Use the logical or (|) function to accumulate the matrices —
dis_threshold=0.4;
a = false(5); % Define Initial 'a'
for n=1:3
xa_time_step=rand(1,5);
za_time_step=rand(1,5);
for k1=1:length(xa_time_step)
for j1=1:length(xa_time_step)
d(j1,k1)=sqrt((xa_time_step(j1)-xa_time_step(k1)).^2+(za_time_step(j1)-za_time_step(k1)).^2);
end
end
d1=abs(d);
d1(d1==tril(d1)) = NaN ;
a_temp = d1 < dis_threshold % Delete Later
a = a | (d1 < dis_threshold)
% disp(a)
end
a_temp = 5×5 logical array
0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
a = 5×5 logical array
0 1 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
a_temp = 5×5 logical array
0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
a = 5×5 logical array
0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0
a_temp = 5×5 logical array
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
a = 5×5 logical array
0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0
.
  10 件のコメント
Ban
Ban 2023 年 10 月 17 日
Thanks.
Star Strider
Star Strider 2023 年 10 月 17 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by