How can I create a loop to check each matrix element?

Question: Design an appropriate parsing strategy among multiple solutions that will go through all 16 possible solutions and discard the invalid ones, therefore reducing the output of the analytical inverse kinematics to a subset of m, with m ϵ [0:16] ( Matlab Script)
This should be written in Matlab
I am trying to write a parsing algorithm that checks values in a 16x6 matrix scanning through the matrix and replacing values, not within the specified range with 180 degrees while keeping the valid values intact
The matrix contains 6 columns which represent the theta 1-6 values and each theta value has its own range i.e theta 1 has a range between -90 to 90, theta 2 has a limit btw -180 to 180 and etc
My design approach so far:
I have defined the specified ranges for the 6 theta values (theta 1- theta 6)
I am trying to write a logic that checks the 16x6 matrix using a for loop and set of If statements( if there is an alternative approach I would gladly appreciate) that would scan through each row and column and check if each value is within its appropriate range and then it outputs an updated matrix with valid values within the specified range and replaces the invalid ones (values not within the specified range with 180 degrees.)
Any sample script/Suggested Approach to this would be appreciated

11 Comments

The MATLAB approach, without using loops and IFs like some low-level programming language:
mxt = [ +90,+180,+200, +32, +99,+123]; % maximum
mnt = [ -90,-180, 0, -23, -99,-321]; % minimum
idx = theta<mnt | theta>mxt;
theta(idx) = 180
I am given a 16x6 matrix and I want to create a loop that would check each element of each row and if 1 element is outside the range, the program will discard that entire row. Therefore, once the loop is completed, it will dispaly a matrix that only contains the rows of elements that fall into the set range. So could you please help me implement that program.
Do you want individual output for all the thetas? Like matrix1 corresponding to theta1, matrix2 for theta2 ...
or Do you want a single output for all thetas?
My bad I wasn't really clear with that last comment. I would like the program to cycle through each row of the given matrix, checking each element and once an element is outside the range, it would displace an unique value such as zero for each element in that specific row. Like I'm trying to create a matlab script that would execute my parsing procedure.
So we have theta1 = [-90,90] and theta2 = [-180 180]
Let the 1st value of the matrix be 120.
It does not come under the criteria of theta1 but it does come under the criteria of theta2, what would you want to then?
If the program detects at least 1 value in that specific row to be out of the range, the entire row should be flagged, meaning giving each element in that row 0. So the first value of each row corresponds to theta 1 criteria and nth value of each row corresponds to the nth theta criteria.
@Omar: you need to be much clearer with your explanation, and not keep chaging what you request. So far you have requested that you want:
  • "...with valid values within the specified range and replaces the invalid ones..."
  • "...it will dispaly a matrix that only contains the rows of elements that fall into the set range"
  • "...the entire row should be flagged, meaning giving each element in that row 0"
So which do you really want: to keep all valid values, or to replace all values in particular rows, or to display something? We cannot guess, when you keep changing your explanation. It just delays getting an answer.
PS: forget about the loop. The loop is your idea of how to write some code, but it is not an explanation of what you are trying to achieve:
I want to keep all valid values
"I want to keep all valid values"
That is what my answer does. Did you try it?
Or does your assignment specify, that a loop is the only way?
The assigment specifies using a loop with if statements
"The assigment specifies using a loop with if statements"
So your teacher wants one simple loop. What have you tried so far?

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 2 Dec 2022

Commented:

on 2 Dec 2022

Community Treasure Hunt

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

Start Hunting!