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
Star Strider 2021년 7월 11일

2 개 추천

The && are ‘short circuit’ AND operators. The second expression (after the &&) will not be evaluated if the first expression is false.
If you want to evaluate both of them, just use & instead.
.

댓글 수: 10

MarshallSc
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
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.
.
MarshallSc
MarshallSc 2021년 7월 11일
Thanks a lot, I will definitely use the method you suggested. Meanwhile, I attached the arguments (A,B and C), so you can see my data.
MarshallSc
MarshallSc 2021년 7월 11일
I used the test, and all of them are logical
Star Strider
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
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!
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
MarshallSc 2021년 7월 12일
Thank you so much buddy. This is an interesting testing method that I will use. Thank you again!
Star Strider
Star Strider 2021년 7월 12일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2021년 7월 11일

댓글:

2021년 7월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by