Help with 6.1.1: Array resizing: Removing elements. in Zybook

39 visualizaciones (últimos 30 días)
Eve
Eve el 15 de Oct. de 2024
Comentada: Umar el 15 de Oct. de 2024
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.
function pendingTasks = RemoveTasks(pendingTasks)
% pendingTasks: Array of tasks ids
pendingTasks=([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
% Remove element 2, 4, and 6 from pendingTasks
pendingTasks([2,4,6]) = [];
end

Respuesta aceptada

Voss
Voss el 15 de Oct. de 2024
pendingTasks is passed as input to the RemoveTasks function, so you shouldn't redefine pendingTasks inside the function.
That is, remove the line
pendingTasks=([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
leaving the RemoveTasks function like this:
function pendingTasks = RemoveTasks(pendingTasks)
% pendingTasks: Array of tasks ids
% Remove element 2, 4, and 6 from pendingTasks
pendingTasks([2,4,6]) = [];
end
and that should be correct.
To test it on the given vectors:
RemoveTasks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
ans = 1×7
1 3 5 7 8 9 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
RemoveTasks([99, 45, 23, 47, 87, 91])
ans = 1×3
99 23 87
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Looks good to me.

Más respuestas (1)

Umar
Umar el 15 de Oct. de 2024

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 comentarios
Eve
Eve el 15 de Oct. de 2024
It worked, Thank you
Umar
Umar el 15 de Oct. de 2024
Hi @Eve,
Glad to know your problem is resolved.

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by