Matlab matrix selection of data using for loops and if statements ,,

In summary: The final x vector will have all the values in it that are greater than or equal to 1 but less than or equal to 0.
  • #1
curious_iza
6
0
matlab matrix selection of data using for loops and if statements,,,,,

Hi people I was wondering if someone could assist me with the following;

You received the following data values from a coworker:
data = [-1.2, 3.3, 5.1, 0.2, 2.9, -0.1];

It turns out that the sensor used to collect the data is prone to noise and the real data should only contain positive even numbers. Write an M-File that rounds the positive values in data to the nearest EVEN integer. For example, 2.5 should be rounded to 2 and 3.1 will be rounded to 4. Negative numbers should be set to 0. HINT: You may want to use if statements and for loops


I can't do this question ... I have having issues with programming and am willing to put in the work to get really good.

My overall pattern of thinking was;

1) Fist of all I know that I want to select the positive answers and I will have to work with either mod/rem to get my remainders. Since I will have to differ between neg & positive I choose rem.

x = rem(data,2) % finds the positive and negetive remainder of the data matrix.

IN THE COMMAND WINDOW THIS GAVE:
x =

-1.2000 1.3000 1.2000 0.2000 0.9000 -0.1000

2) Ok ... so I need to get rid of the negs...
but if I go x > 0 then I will just get
0 1 1 1 1 0
and the values of my remainder are gone

I am really lost with this questions and it is meant to be 'easy' ... sigh ... I have played around for almost 3 HOURS and cannot get it... Please help me...

Thankyou
 
Physics news on Phys.org
  • #2


So you have the groundwork laid out for one possible solution - keep going along this path and you'll get there soon.

First, a comment about:

curious_iza said:
2) Ok ... so I need to get rid of the negs...
but if I go x > 0 then I will just get
0 1 1 1 1 0
and the values of my remainder are gone

What is happening here is your x vector is being tested against 0 element-wise. Each element is either greater than 0 (denoted by 1, also known as true) or not (denoted by 0, also known as false). So basically, MATLAB is telling you, for each element, if it is greater than 0 or not. (Now the output should make sense.)

Going forward for a first implementation, you should take the hint given in the problem: use a for loop and if statements. Basically, you need to loop through each element of your input vector and perform an "if" check. Then there are two cases denoted by 1 (true) or 0 (false). Based off if x is greater than 0 or not, you can do what you need to in each case. Note that you should probably try to create a simple loop and process each element in the vector independently (vector elements can be accessed by x(i)).

Hopefully the comment about why you are getting the 0s and 1s in your point number 2 will help move you along some more.
 
  • #3


Hi I am still nowhere with this question;;;; GRRR so frustrating!

I understand that;
1) I need to use rem(data,2) to find the remainder and that;
if the remainder is positive and the value of it is greater then 1 then I must take the ceil(data)
2) If rem(data,2) is less then 1 but greater then 0 I must take a floor(data)
3) If rem(data,2) is negetive that data value must be 0.

I CANT put this into a code. I just don't understand how to do this in MATLAB I have been trying for 2 days now... can someone please help me? Thankyou so much!
 
  • #4


curious_iza said:
Hi I am still nowhere with this question;;;; GRRR so frustrating!

I understand that;
1) I need to use rem(data,2) to find the remainder and that;
if the remainder is positive and the value of it is greater then 1 then I must take the ceil(data)
2) If rem(data,2) is less then 1 but greater then 0 I must take a floor(data)
3) If rem(data,2) is negetive that data value must be 0.

I CANT put this into a code. I just don't understand how to do this in MATLAB I have been trying for 2 days now... can someone please help me? Thankyou so much!

You just need to put it all inside of a case statement.

so after you run rem, you have another vector, which you called "x" above

for r=1:length(x)
if x(r)<0
x(r)=0;
elseif x(r)>0 && x(r)<1
x(r)=floor(x(r));
else
x(r)=ceil(x(r));
end
end

sorry its kind of hard to see, I don't know how to indent.

If "x" is run through that statement, it should modify it to your specifications above (assuming I understood what you were saying there ;) ). I hope this helps.
 
Last edited:
  • #5


I know it's too late to help the questioner with their homework, but I found this question in a web search and felt compelled to comment... I'm sorry, but the instructor should be flogged using something substantially firmer than a wet noodle. MATLAB is MATrix LABratory - it is optimized to deal with matricies, so use matrices and vectors whenever possible, not for loops with if statements - that's the domain of C and FORTRAN. there are times to use for loops and if statements in matlab, but this is most emphatically not one of them. I just got done taking a doubly nested for loop (indexing into a matrix) out of some one else's subroutine and sped it up from taking 120 seconds to execute down to just under 3 seconds (2.78 seconds, actually). just a slight improvement in execution time (over 40x or 1.6 orders of magnitude!).

the programming clues in the program assignment are:
- eliminate negative values
- round to even numbers (hard to do, unless you think first)

data=[-1.2, 3.3, 5.1, 0.2, 2.9, -0.1]

mask = (data>=0) % 0 for negative, 1 for non-negative (0 and positives)
limited = mask.*data % positive values unchanged, negatives are now zero
rounded = 2*round(limited/2) % round to even numbers

rounded = 0 4 6 0 2 0


it really is that easy, and I've only been using MATLAB for a few weeks. think first, then code. trust me, you'll like the results much better. and use sensible variable names and comment your code - you'll appreciate it when you go back to your code after a hard Friday night... 8) don't ask me how I know... 8)

oh, and here's a one line version... rounded = 2*round(((data>=0).*data)/2)
 

Related to Matlab matrix selection of data using for loops and if statements ,,

What is Matlab matrix selection?

Matlab matrix selection is the process of accessing and retrieving specific data from a matrix using for loops and if statements. It allows you to filter and manipulate large amounts of data efficiently.

How do I select data from a matrix in Matlab using for loops and if statements?

To select data from a matrix in Matlab, you can use a combination of for loops and if statements. The for loop will iterate through the matrix, while the if statement will check for a specific condition and retrieve the desired data.

What is the role of for loops in Matlab matrix selection?

For loops are used in Matlab matrix selection to iterate through each element in a matrix and perform a specific task, such as checking for a condition or retrieving data. They are essential for efficient data manipulation and selection.

How do if statements help in Matlab matrix selection?

If statements are used in Matlab matrix selection to check for specific conditions and retrieve data that meets those conditions. They allow you to filter and select data based on certain criteria, making data manipulation more precise and efficient.

Can I use both for loops and if statements for Matlab matrix selection at the same time?

Yes, you can use for loops and if statements together for Matlab matrix selection. In fact, combining these two elements is essential for efficient and precise data selection and manipulation in Matlab.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
841
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
992
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
986
Back
Top