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 件のコメント
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);
Star Strider
2023 年 10 月 16 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
