If statement for cells
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi, I have 3 different cell arrays like below - same size (let's call them A, B and C):

That I need to evaluate in an if statement in a for loop. But the final result gives me several empty numeric array in couple of the cells. The code is:
for i=1:4
for j=1:4
for k=1:10
for r=1:10
if ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))<1) && ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))<1)
A_num{i,j}{k,r}='Point is stable: Category 1 is assigned';
elseif ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))>1) && ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))>1)
A_num{i,j}{k,r}='Point is unstable';
end
end
end
end
end
For example, one of the cell array result is like bleow:

I checked all the components of the original cell arrays and none of them are zero or inf. Am I doing something wrong here?
採用された回答
Star Strider
2021 年 7 月 11 日
10 件のコメント
MarshallSc
2021 年 7 月 11 日
Thanks, I already tried using & instead of && in my initial trial as there were similiar topics that folks encountered the same issue, but I still get the same results.
MarshallSc
2021 年 7 月 11 日
編集済み: MarshallSc
2021 年 7 月 11 日
Also, when I want to perform further calculation in the code, the results get more illogical:
for i=1:4
for j=1:4
for k=1:10
for r=1:10
if ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))<1) & ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))<1)
A_num{i,j}{k,r}='Point is stable: Category 1 is assigned';
elseif ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))>1) & ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))>1)
A_num{i,j}{k,r}='Point is unstable';
if abs(C{i,j}(k,r))>abs(B{i,j}(k,r))
M{i,j}(k,r)=abs(C{i,j}(k,r));
else
M{i,j}(k,r)=abs(B{i,j}(k,r));
end
end
end
end
end
end
The result for M:

While the M should store either a value from B or C because they have to be either smaller or greater than each other. Has anyone stumbled upon an issue like this?
Star Strider
2021 年 7 月 11 日
My pleasure!
Something is clearly wrong with the logic.
I suggest that you break out the comparison statements into separate statements to check to be sure that they are doing what you want them to do. Just add the separate staements before the if block. (I call them ‘Test1’ and ‘Test2’ here.) It will not be necessary to change the code otherwise.
Example —
for i=1:4
for j=1:4
for k=1:10
for r=1:10
Test1 = ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))<1)
Test2 = ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))<1)
if ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))<1) && ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))<1)
A_num{i,j}{k,r}='Point is stable: Category 1 is assigned';
elseif ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))>1) && ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))>1)
A_num{i,j}{k,r}='Point is unstable';
if abs(C{i,j}(k,r))>abs(B{i,j}(k,r))
M{i,j}(k,r)=abs(C{i,j}(k,r));
else
M{i,j}(k,r)=abs(B{i,j}(k,r));
end
end
end
end
end
end
That should tell you if they are doing what you want them to do. You can slso subscript them:
Test1(k,r) = ((abs(A{i,j}(k,r)))/(abs(C{i,j}(k,r)))<1);
Test2(k,r) = ((abs(A{i,j}(k,r)))/(abs(B{i,j}(k,r)))<1);
so you can look at them in detail later to see how they compare to what you want them to do. (This is how I always solve such logic problems in code I write.)
I believe the comparison ‘<1’ (or ‘>1’ in the subsequent test) should be outside all the nested parentheses, however since I do not understand what you are doing, I cannot be certain of that.
.
MarshallSc
2021 年 7 月 11 日
MarshallSc
2021 年 7 月 11 日
I used the test, and all of them are logical
Star Strider
2021 年 7 月 11 日
I expect them to be logical.
Do they do what you want them to do?
You may have to examine the components of the statements as well.
MarshallSc
2021 年 7 月 11 日
Thank you. The statement is designed to evaluate each point of the matrices in the cell array and determine whether they are stable or not. I thought some ratios results that are used do not necessarily fall under the two statements, (a point is not both A/B >1 and A/C >1 OR A/B <1 and A/C<1, and I suspected maybe that's why the empty numeric array is produced). But when I test them individually, they are categorized under one of the statements. Another issue that I don't understand is why the results in M are mostly empty numeric arrays while the statements for each point has to render a logical value and be stored in M (either smaller or larger). I guess I have to dig deeper into the root cause. But I very much appreciate your time and valuble advices. Thank you!
Star Strider
2021 年 7 月 11 日
I do not see anything wrong with the logic itself, however you may want to use or,| rather than and,& in the comparison:
Compare the results of using ‘&’ and ‘|’ in ‘Test12’ here:
LD1 = load('A.mat');
A = LD1.Kappa_x;
LD2 = load('B.mat');
B = LD2.KappaShort_x;
LD3 = load('C.mat');
C = LD3.KappaLong_x;
for i = 1:4
for j = 1:4
Quot1{i,j} = abs(A{i,j})./abs(C{i,j});
Quot2{i,j} = abs(A{i,j})./abs(B{i,j});
end
end
figure
[x,y] = ndgrid(1:10);
k = 0;
for i = 1:4
for j = 1:4
k = k + 1;
Test1 = Quot1{i,j}<1;
Test2 = Quot2{i,j}<1;
Test12 = Test1 | Test2;
subplot(4,4,k)
plot3(x, y, Test1, '+b')
hold on
plot3(x, y, Test2, 'xr')
plot3(x, y, Test12, 'og')
hold off
grid
view(-40,10)
zlim([-0.5 1.5])
title(sprintf('i=%d, j=%d',i,j))
set(gca, 'ZTick',[0 1], 'ZTickLabel',["F","T"])
end
end
pos = get(gcf,'Position');
set(gcf, 'Position',pos+[0 -500 500 500])
I always encourage plotting if possible, since it is an easy way to see what the code is actually doing:


.
MarshallSc
2021 年 7 月 12 日
Thank you so much buddy. This is an interesting testing method that I will use. Thank you again!
Star Strider
2021 年 7 月 12 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で MATLAB についてさらに検索
タグ
参考
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)
