Help with 6.1.1: Array resizing: Removing elements. in Zybook
0 commentaires
Réponse acceptée
2 commentaires
Plus de réponses (1)
Hi @Eve,
You asked, “Array resizing: Removing elements. Need Check if RemoveTasks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) returns [1, 3, 5, 7, 8, 9, 10] & Check if RemoveTasks([99, 45, 23, 47, 87, 91]) returns [99, 23, 87] Remove elements 2, 4, and 6 from row array pendingTasks.”
To achieve the desired functionality, a MATLAB function named RemoveTasks is defined in the script provided below. This function will accept an array of task IDs and will remove the specified elements (2, 4, and 6) from the array. Below is the complete code for the function, along with explanations of each part.
function pendingTasks = RemoveTasks(pendingTasks) % RemoveTasks: Function to remove specific task IDs from the array. % Input: % pendingTasks: Array of task IDs (1D array) % Output: % pendingTasks: Modified array with specified elements removed
% Display the original array for reference disp('Original pendingTasks:'); disp(pendingTasks);
% Define the indices of elements to be removed indicesToRemove = [2, 4, 6];
% Remove the specified elements from the pendingTasks array pendingTasks(indicesToRemove) = [];
% Display the modified array after removal disp('Modified pendingTasks after removal:'); disp(pendingTasks); end
Please see attached.
So, in the above script, function RemoveTasks is defined with one input parameter, pendingTasks, which is expected to be a 1D array of task IDs. The original array is displayed using disp for clarity, allowing you to see the input before any modifications are made. An array indicesToRemove is created, containing the indices of the elements that need to be removed from the pendingTasks array. In this case, you are removing the elements at positions 2, 4, and 6. The specified elements are removed from the pendingTasks array using the syntax pendingTasks(indicesToRemove) = [];. This effectively deletes the elements at the specified indices. After the removal operation, the modified array is displayed to show the result of the function.To verify that the function works as intended, you can call it with the specified test cases as mentioned in your comments.
Hope this helps.
Please let me know if you have any further questions.
2 commentaires
Voir également
Catégories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!