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?

采纳的回答

Voss
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
matriz_viaveis = 5×4
1.0e+05 * NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.1364 1.1770 1.2348

更多回答(1 个)

Torsten
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by