overlap logical matrices in MATLAB
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
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
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
2023년 10월 4일
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
2023년 10월 16일
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.
.
Ban
2023년 10월 16일
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.
Star Strider
2023년 10월 16일
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.
Ban
2023년 10월 16일
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
2023년 10월 16일
편집: Star Strider
2023년 10월 17일
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.
.
Ban
2023년 10월 17일
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.
Star Strider
2023년 10월 17일
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?
.
Ban
2023년 10월 17일
Thanks.
Star Strider
2023년 10월 17일
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
