Store elements of a matrix but ONLY IF the respective element of ANOTHER matrix meets condition.
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm storing the results of my calculations on 3 matrices. Each element of those matrices represents a scenario.
% Price
matriz_custo =
200.2634e+003 112.7627e+003 89.3714e+003 81.6665e+003
98.2240e+003 65.6027e+003 60.1839e+003 61.4368e+003
77.1716e+003 61.2042e+003 61.0373e+003 64.8769e+003
86.9063e+003 76.7914e+003 78.3755e+003 83.0625e+003
113.2236e+003 113.6395e+003 117.6975e+003 123.4758e+003
% Condition 1
matriz_regtensao =
2.5321e+000 1.9150e+000 1.7125e+000 1.5853e+000
1.5215e+000 1.2673e+000 1.1857e+000 1.1354e+000
1.2237e+000 1.0788e+000 1.0328e+000 1.0047e+000
1.1203e+000 1.0133e+000 979.4391e-003 958.8489e-003
927.2241e-003 888.9708e-003 876.9749e-003 869.7100e-003
% Condition 2
matriz_angulodecarga =
46.4489e+000 42.1258e+000 39.9564e+000 38.1092e+000
35.2673e+000 29.8403e+000 27.4642e+000 25.6752e+000
27.7915e+000 22.6781e+000 20.5982e+000 19.1148e+000
23.9977e+000 19.2834e+000 17.4239e+000 16.1276e+000
13.4338e+000 10.4040e+000 9.2731e+000 8.5272e+000
I need to select viable scenarios from the Price matrix and store them, but only if the respective element of the other matrices meet a certain condition, namely:
Condition 1: N < 0.9
Condition 2: N < 30
I tried using the code bellow but the resulting "matriz_viaveis" is always empty.
if ((matriz_regtensao(x,y)) < 30 & (matriz_angulodecarga(x,y) < 0.9))
matriz_viaveis(x,y) = matriz_custo(x,y)
end
I know there are viable scenarios but they are never stored. The x,y variables represent the column/line and are used in the calculations prior to this, I don't think it's relevant though.
How would you do this? Is there a better way of storing these results?
댓글 수: 0
채택된 답변
Voss
2022년 3월 23일
It looks like matriz_angulodecarga has no elements that are less than 0.9. Is it possible the conditions got swapped inadvertently?
I notice that the comment says "% Condition 2" for matriz_angulodecarga, and Condition 2 is "Condition 2: N < 30".
When I swap the conditions, I get some values in matriz_viaveis:
% Price
matriz_custo = [
200.2634e+003 112.7627e+003 89.3714e+003 81.6665e+003
98.2240e+003 65.6027e+003 60.1839e+003 61.4368e+003
77.1716e+003 61.2042e+003 61.0373e+003 64.8769e+003
86.9063e+003 76.7914e+003 78.3755e+003 83.0625e+003
113.2236e+003 113.6395e+003 117.6975e+003 123.4758e+003];
% Condition 1
matriz_regtensao = [
2.5321e+000 1.9150e+000 1.7125e+000 1.5853e+000
1.5215e+000 1.2673e+000 1.1857e+000 1.1354e+000
1.2237e+000 1.0788e+000 1.0328e+000 1.0047e+000
1.1203e+000 1.0133e+000 979.4391e-003 958.8489e-003
927.2241e-003 888.9708e-003 876.9749e-003 869.7100e-003];
% Condition 2
matriz_angulodecarga = [
46.4489e+000 42.1258e+000 39.9564e+000 38.1092e+000
35.2673e+000 29.8403e+000 27.4642e+000 25.6752e+000
27.7915e+000 22.6781e+000 20.5982e+000 19.1148e+000
23.9977e+000 19.2834e+000 17.4239e+000 16.1276e+000
13.4338e+000 10.4040e+000 9.2731e+000 8.5272e+000];
[nx,ny] = size(matriz_custo);
% initialize matriz_viaveis to be a matrix the same size as the other
% matrices, with all NaNs:
matriz_viaveis = NaN(nx,ny);
for x = 1:nx
for y = 1:ny
if matriz_regtensao(x,y) < 0.9 && matriz_angulodecarga(x,y) < 30
% if matriz_regtensao(x,y) < 30 && matriz_angulodecarga(x,y) < 0.9
matriz_viaveis(x,y) = matriz_custo(x,y);
end
end
end
matriz_viaveis
댓글 수: 2
추가 답변 (1개)
Torsten
2022년 3월 23일
matriz_viaveis = NaN(size(matriz_angulodecarga));
for x= 1:size(matriz_angulodecarga,1)
for y = 1:size(matriz_angulodecarga,2)
if (matriz_regtensao(x,y) < 30) & (matriz_angulodecarga(x,y) < 0.9)
matriz_viaveis(x,y) = matriz_custo(x,y)
end
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!