If statement for cells
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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일
2 개 추천
댓글 수: 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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
