Question
Hello, I baffled by one task in Matlab, namely I need to create two vectors that contains all positive and all negative values of an initial vector. I would like to get a general answer to my question. Here is my script (unfinished): for i=1:numel(x) if x(i) > 0 & x(i)==0 P(i)=????????(all positives) end end disp('The postive vector is:')
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 31, 2026
You don't need a loop, because logical indexing is much simpler and faster:
>> X = [1,2,-3,-4,5,-6,7,-8,9]; >> idx = X<0; % create logical index >> neg = X(idx) neg = -3 -4 -6 -8 >> pos = X(~idx) pos = 1 2 5 7 9
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related vectors in matlab Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →