ifelse and switch don't match

1 view (last 30 days)
Hi, my problem is this code:
if A21 == N2
Re21 = N2r;
elseif A21 == I2
Re21 = I2r;
elseif A21 == R2
Re21 = R2r;
elseif A21 == T2
Re21 = T2r;
end
I2 and R2 have the same value so, when I put A21 = R2, the value of Re21 is I2r because MATLAB thinks that I2 and R2 are the same variable, It only took the first value in the code (I2). I need that MATLAB recognize that Re21 is R2r. How can I fix it? Same isue with Switch.
  4 Comments
Ignacio Brown Vidal
Ignacio Brown Vidal on 27 May 2022
The R2, N2, T2 and I2 represents state of a line of electrical distribution system, A21 and Re21 are part of diferents matrix (6x6). So, deppending of the state, I put R2, N2 T2 or I2 until I complete the matrix. My error was that I put values for the states variables so, in case that two or more of the states have the same value, MATLAB will always match the first statement of the IF ELSEIF, in this case, Re21 = I2r, when I need the statemen Re21 = R2r. In order to make MATLAB use the statement I need, I make R2, N2, T2 and I2 symbolic variables, so when I put A21=R2, MATLAB will use the statement R21 = R2r.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 27 May 2022
Based on your prose description, it sounds like your underlying task is to populate the matrix Re21 based on the contents of the matrix A21. Is that correct? If so you could consider either logical indexing or the discretize function depending on whether the states in A21 are discrete or continuous.
A21 = randi(10, 5, 5) % Sample data
A21 = 5×5
3 8 2 4 8 6 9 5 1 7 3 1 7 4 8 9 1 5 6 5 7 8 8 2 1
Re21 = zeros(size(A21)) % Sample target
Re21 = 5×5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Re21(A21 == 1) = 11
Re21 = 5×5
0 0 0 0 0 0 0 0 11 0 0 11 0 0 0 0 11 0 0 0 0 0 0 0 11
Re21(A21 == 3) = 333
Re21 = 5×5
333 0 0 0 0 0 0 0 11 0 333 11 0 0 0 0 11 0 0 0 0 0 0 0 11
Re21(A21 == 7) = NaN
Re21 = 5×5
333 0 0 0 0 0 0 0 11 NaN 333 11 NaN 0 0 0 11 0 0 0 NaN 0 0 0 11
Re21(A21 == 9) = -1
Re21 = 5×5
333 0 0 0 0 0 -1 0 11 NaN 333 11 NaN 0 0 -1 11 0 0 0 NaN 0 0 0 11
Or for the continuous case, where values in A21 in the range [0, 3) should become -1 in Re21, values in [3, 7) should become 2, values in [7, 8) should become -33, and values in [8, 10] should become 444:
A21 % Display for reference
A21 = 5×5
3 8 2 4 8 6 9 5 1 7 3 1 7 4 8 9 1 5 6 5 7 8 8 2 1
Re21_2 = discretize(A21, [0 3 7 8 10], [-1, 2, -33, 444])
Re21_2 = 5×5
2 444 -1 2 444 2 444 2 -1 -33 2 -1 -33 2 444 444 -1 2 2 2 -33 444 444 -1 -1
Now neither of these would make elements in Re21 take on two different values if the value in an element of A21 matches two of your conditions. As a concrete example if you wanted Re21 to be 1 if the value in A21 was in the range [3, 6) and 2 if it was in the range [4, 7) then you have to choose if it should be 1 or 2 if the value in A21 is 5. It can't be both. [Those overlapping ranges might also be a code smell.]

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by