overlap logical matrices in MATLAB

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 Comments

"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?
What do you mean with "overlapped logical matrixp"? is it the & operator of the 3 matrices created in the loop?

Sign in to comment.

 Accepted Answer

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 Comments

The code you suggested work. But I am facing one more problem. In the below code I need to make a matrix of the quantity 'encounter_unique'. However I noticed that when I run it for individual values of 'ecc', I get different values compared to when I run for all the values of 'ecc' and 'vp'. Somewhere value of values of 'encounter_unique' are getting added up between varying values of 'ecc' and 'vp'. Please suggest.
This is merely a representative code for the sake of brevity (not the original one).
clear;
close all;
clc;
n_vp=3;
n_ecc=2;
vp_matrix=linspace(0.115*10^-1, 6*10^-3, n_vp); % Particle intrinsic velocity
ecc_matrix=linspace(-0.9,-0.1,n_ecc);
n_encounter_rate=1 ;
outdata = cell(length(vp_matrix), length(ecc_matrix));
for jecc=1:length(ecc_matrix);
for jvp=1:length(vp_matrix);
ecc=ecc_matrix(jecc) ;
vp=vp_matrix(jvp) ;
dis_threshold=0.03;
a = false(5); % Define Initial 'a'
for n=1:3
xa_time_step=ecc*rand(1,5);
za_time_step=vp*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 = a | (d1 < dis_threshold);
encounter_unique=sum(sum(a));
end
[outdata(n_encounter_rate)] = {encounter_unique};
n_encounter_rate=n_encounter_rate+1 ;
end
end
encounter_rate_matrix=cell2mat(outdata);
I am having problems understanding what you are doing. The sum function will do exactly what you ask it to, and here it is summing the number of logical 1 (true) values in the ‘a’ matrix.
n_vp=3;
n_ecc=2;
vp_matrix=linspace(0.115*10^-1, 6*10^-3, n_vp); % Particle intrinsic velocity
ecc_matrix=linspace(-0.9,-0.1,n_ecc);
n_encounter_rate=1 ;
outdata = cell(length(vp_matrix), length(ecc_matrix));
for jecc=1:length(ecc_matrix);
for jvp=1:length(vp_matrix);
ecc=ecc_matrix(jecc) ;
vp=vp_matrix(jvp) ;
dis_threshold=0.03;
a = false(5); % Define Initial 'a'
for n=1:3
xa_time_step=ecc*rand(1,5);
za_time_step=vp*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 = a | (d1 < dis_threshold)
encounter_unique=sum(sum(a))
end
[outdata(n_encounter_rate)] = {encounter_unique};
n_encounter_rate=n_encounter_rate+1 ;
end
end
a = 5×5 logical array
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 1
a = 5×5 logical array
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 1
a = 5×5 logical array
0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 2
a = 5×5 logical array
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 1
a = 5×5 logical array
0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 2
a = 5×5 logical array
0 0 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 3
a = 5×5 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 0
a = 5×5 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 0
a = 5×5 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 0
a = 5×5 logical array
0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
encounter_unique = 4
a = 5×5 logical array
0 1 1 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
encounter_unique = 7
a = 5×5 logical array
0 1 1 0 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0
encounter_unique = 8
a = 5×5 logical array
0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
encounter_unique = 2
a = 5×5 logical array
0 1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0
encounter_unique = 9
a = 5×5 logical array
0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0
encounter_unique = 10
a = 5×5 logical array
0 1 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
encounter_unique = 3
a = 5×5 logical array
0 1 1 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0
encounter_unique = 8
a = 5×5 logical array
0 1 1 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0
encounter_unique = 8
encounter_rate_matrix=cell2mat(outdata)
encounter_rate_matrix = 3×2
2 8 3 10 0 8
I am lost.
I do not understand what ‘ecc’ and ‘vp’ are, or what they do.
.
xa_time_step=ecc*rand(1,5);
za_time_step=vp*rand(1,5);
Here ecc and vp are included. Actually encounter_unique varies with ecc and vp.
I am still lost.
What is not working?
Specifically, what is your code doing now and what would you want it to do? Examples from it would help me understand.
Please run the attached file. The target is to get Enc_rate_matrix (the last line).
If I run the code for individual values of 'ecc' (say putting ecc=0.1 and 0.99), then I do not get back the column vectors of the Enc_rate_matrix. Instead I get different values. When I run the full code i.e. varying both ecc and vp (line: 45-46), the values seem to be more than expected.
Star Strider
Star Strider on 16 Oct 2023
Edited: Star Strider on 17 Oct 2023
I ran it, and it runs without error, however I still do not understand what the problem is.
What is the code supposed to do? It would really help me to know that.
What is it doing that you do not want it to do, or what is it not doing that you want it to do?
I would like to help you to get it to do what you want it to do, however I do not understand what it is supposed to do, what ‘ecc’ or ‘vp’ and ‘Enc_rate_matrix’ are (specifically what they represent), or what the output is supposed to be for various values of ‘ecc’ and ‘vp’.
EDIT — (17 Oct 2023 at 00:48)
What would is the code producing now and what would you want it to produce? (I do not need to know the physics, simply what the code does now and what you want it to do.) I do not understand what the problem is with it.
.
I have added one different .m file. I am rephrasing my problem.
Please run the code. The code runs for 3 values of vp and 2 values of ecc (line: 45-46). Now, in the code, I have kept
ecc_matrix_1=[0.1, 0.1] (line: 33) that makes ecc= 0.1 & 0.1 (line: 53). This means I am running the code to get 'enc_rate_matrix' (line: 188), which is a 3 X 2 matrix. In this matrix both the column should be same since the code is run for ecc_matrix_1=[0.1, 0.1] (line: 33), where both the values are same, i.e. 0.1.
However, enc_rate_matrix turns out to be:
99    206
163   206
206   206
Here 2nd column should be also [99 163 206]. sO I want the code to be such that enc_rate_matrix turns out to be:
99    99
163   163
206   206
I hope this clarifies the issue.
It definitely does, and the fix is straightforward!
I ran it in MATLAB Online. It turns out that you need to reset ‘a_rad’ in each ‘jecc’ iteration. Then, it works as desired:
for jecc=1:length(ecc_matrix_1)
a_rad = false(N); % < RESET In Each Iteration
It is not necessary to post any of the rest of the code, since this is the only change necessary.
That gives the desired result for:
ecc_matrix_1=[0.1, 0.1]
That should also work correctly for other values in ‘ecc_matrix_1’ so just for grins, I set:
ecc_matrix_1=[0.1, 0.2, 0.3]
and got these results:
enc_rate_matrix =
99 55 47
163 116 100
206 162 146
Does that seem reasonable?
.
Thanks.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Asked:

Ban
on 4 Oct 2023

Commented:

on 17 Oct 2023

Community Treasure Hunt

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

Start Hunting!