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

Click For Summary
SUMMARY

The discussion focuses on using MATLAB to select and process data from a matrix, specifically rounding positive values to the nearest even integer while setting negative values to zero. The provided data set is data = [-1.2, 3.3, 5.1, 0.2, 2.9, -0.1]. Participants suggest using the rem function to identify remainders, along with for loops and if statements to implement the logic. A more efficient solution is proposed using matrix operations, which significantly improves execution time.

PREREQUISITES
  • Understanding of MATLAB syntax and functions
  • Familiarity with matrix operations in MATLAB
  • Knowledge of conditional statements and loops
  • Basic understanding of rounding functions like ceil and floor
NEXT STEPS
  • Explore MATLAB's matrix manipulation capabilities
  • Learn about vectorized operations in MATLAB for performance optimization
  • Research the use of logical indexing in MATLAB
  • Practice implementing if statements and for loops in MATLAB
USEFUL FOR

Students learning MATLAB, data analysts working with numerical data, and programmers seeking to optimize their MATLAB code for performance.

curious_iza
Messages
6
Reaction score
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


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.
 


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!
 


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:


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)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
2
Views
1K