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

조회 수: 42 (최근 30일)
Eve
Eve 2024년 10월 15일
댓글: Umar 2024년 10월 15일
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

채택된 답변

Voss
Voss 2024년 10월 15일
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.
  댓글 수: 2
Eve
Eve 2024년 10월 15일
@Voss Thank you for your help. It worked.
Voss
Voss 2024년 10월 15일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Umar
Umar 2024년 10월 15일

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
Eve
Eve 2024년 10월 15일
It worked, Thank you
Umar
Umar 2024년 10월 15일
Hi @Eve,
Glad to know your problem is resolved.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by