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 个评论
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?
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.
.
I used the test, and all of them are logical
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.
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!
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:


.
Thank you so much buddy. This is an interesting testing method that I will use. Thank you again!
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!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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)
