Code for filtering through elements of an array depending on criteria?

1 view (last 30 days)
I have 256 different combinations which are all the possible resulting combinations of four modes: Mode1, Mode2, Mode3 and Mode4, which can all be in either states: A, B, C or D. 4^4=256 hence the possible 256 different combinations. Through the use of the Kronecker Product, I know the exact order of these combinations.
Each combination corresponds to the respective element in a 1 by 256 array, lets say called INITIAL
The user can input the initial combination through four drop down lists, (one for each mode), and select one of the four states for each mode. E.g Mode1=C, Mode2=A, Mode3=C and Mode4=D.
Using IF functions, how can I select the exact element in the array. The element selection conditions are listed below. Please can someone help me with the coding for actually selecting the element?
IF Mode1 = A % Filter elements to (1:4:256) i.e 1, (multiples of 4)+1, up to 256
ELSE IF Mode1 = B % Filter elements to (2:4:256) i.e 2, (multiples of 4)+2, up to 256
ELSE IF Mode1 = C % Filter elements to (3:4:256) i.e 3, (multiples of 4)+3, up to 256
ELSE IF Mode1 = D % Filter elements to (4:4:256) i.e multiples of 4, up to 256
END
IF Mode2 = A % Further filter elements to (1:16:256)+(0:3) i.e 1-4,17-20,etc, up to 256
ELSE IF Mode2 = B % Further filter elements to (5:16:256)+(0:3) i.e 5-8,21-24,etc, up to 256
ELSE IF Mode2 = C % Further filter elements to (9:16:256)+(0:3) i.e 9-12,25-28,etc, up to 256
ELSE IF Mode2 = D % Further filter elements to (13:16:256)+(0:3) i.e 13-16,29-32,etc, up to 256
END
IF Mode3 = A % Further filter elements to (1:64:256)+(0:15) i.e 1-16,65-80,etc, up to 256
ELSE IF Mode3 = B % Further filter elements to (17:64:256)+(0:15) i.e 17-32,81-96,etc, up to 256
ELSE IF Mode3 = C % Further filter elements to (33:64:256)+(0:15) i.e 33-48,97-112,etc, up to 256
ELSE IF Mode3 = D % Further filter elements to (49:64:256)+(0:15) i.e 49-64,113-128,etc, up to 256
END
IF Mode4 = A % Further filter elements to (1:64) i.e 1-64
ELSE IF Mode4 = B % Further filter elements to (65:128) i.e 65-128
ELSE IF Mode4 = C % Further filter elements to (129:192) i.e 129-192
ELSE IF Mode4 = D % Further filter elements to (193:256) i.e 193-256
END
% N = The single element from INITIAL that fits the above criteria based on user input
Therefore, after filtering for Mode1, 64 combinations will remain.
after filtering for Mode2, 16 combinations will remain.
after filtering for Mode3, 4 combinations will remain.
and after filtering for Mode2, 1 combination will remain.
Please can someone help me with the filtering aspect of this?
Thank you

Answers (1)

Mehmed Saad
Mehmed Saad on 15 Apr 2020
Use of switch case is better
Mode1 = 'A'; % can be B C D
Mode2 = 'A'; % can be B C D
Mode3 = 'A'; % can be B C D
Mode4 = 'A'; % can be B C D
data = rand(1,256);
switch Mode1
case 'A'
hakuna_matata1 = data(1:4:end);
case 'B'
hakuna_matata1 = data(2:4:end);
case 'C'
hakuna_matata1 = data(3:4:end);
case 'D'
hakuna_matata1 = data(4:4:end);
end
switch Mode2
case 'A'
hakuna_matata2 = data(1:16:end);
case 'B'
hakuna_matata2 = data(5:16:end);
case 'C'
hakuna_matata2 = data(9:16:end);
case 'D'
hakuna_matata2 = data(13:16:end);
end
switch Mode3
case 'A'
hakuna_matata3 = data(1:64:end);
case 'B'
hakuna_matata3 = data(17:64:end);
case 'C'
hakuna_matata3 = data(33:64:end);
case 'D'
hakuna_matata3 = data(49:64:end);
end
switch Mode4
case 'A'
hakuna_matata4 = data(1:256:end);
case 'B'
hakuna_matata4 = data(65:256:end);
case 'C'
hakuna_matata4 = data(129:256:end);
case 'D'
hakuna_matata4 = data(193:256:end);
end
  4 Comments
Ashton Linney
Ashton Linney on 15 Apr 2020
Hi Muhammad, thank you for your answer!
It is definitely closer to the solution than I was however I still am have an issue with it.
Looking at your original answer if we replace line 5 with:
data = (1:256);
Thereby setting the value of each sell to the number combination it is. E.g column 138 has a value of 138.
When running the code with Mode1=A, Mode2=A, Mode3=A and Mode4=A, I get the correct answer output. That is hakuna_matata1,2,3 and 4 all give the correct cells specifies by the criterial. With hakuna_matata4 giving an output of 1. Which is correct.
If I then change it such that Mode1=B, Mode2=A, Mode3=A and Mode4=A, hakuna_matata1 gives the correct output (the correctly slected 64 cells). However none of the other three give the correct output. They stay the same as if all four modes are in state A. E.g. hakuna_matata2 should output (2 18 34 etc) but it still only outputs (1 17 33 etc) as if it has not been updated by the new effect of hakuna_matata1.
Do you know why this may be?
Mehmed Saad
Mehmed Saad on 16 Apr 2020
Edited: Mehmed Saad on 16 Apr 2020
E.g. hakuna_matata2 should output (2 18 34 etc) but it still only outputs (1 17 33 etc) as if it has not been updated by the new effect of hakuna_matata1.
  1. it should give you (5 21 37 etc)
  2. I run the code with following condition
Mode1 = 'A'; % can be B C D
Mode2 = 'B'; % can be B C D
Mode3 = 'C'; % can be B C D
Mode4 = 'D'; % can be B C D
The outputs are
hakuna_matata1 =
[1 5 9 13 17 21 25 , ... 253]
hakuna_matata2 =
[5 21 37 53 69 85 , ... 245]
hakuna_matata3 =
[33 97 161 225]
hakuna_matata4 =
[193]
I think you are making some mistake while runing the code

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!