Im trying to fix a code in my home work, the directions state to Write a *single* statement that shifts row array attendanceValues one position to the left. The rightmost element in shiftedValues also keeps its value. any thoughts

10 Ansichten (letzte 30 Tage)
function attendanceValues = ShiftValues(attendanceValues)
% attendanceValues: Array contains 4 elements recording attendence for the last 4 shows
% Change indexing values
attendanceValues(1:4) = attendanceValues(2:4);
attendanceValues = circshift(attendanceValues,-1)
end
  1 Kommentar
zachary stallings
zachary stallings am 29 Mär. 2017
i figured it out all i needed to do was chance the indexing values to: attendanceValues(1:1) = attendanceValues(2:2); attendanceValues(2:2) = attendanceValues(3:3); attendanceValues(3:4) = attendanceValues(4:4);

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 29 Mär. 2017
attendanceValues(1:4) = attendanceValues(2:4);
has four output locations, #1, #2, #3, #4, but only 3 input locations, #2, #3, and #4 . That statement cannot succeed.
If you want to copy 3 input values then you need 3 output locations.
  5 Kommentare
DGM
DGM am 8 Feb. 2024
We assume that the vector always contains exactly 4 elements. The vector is left shifted such that the last element is retained, and the first element is discarded. If the input vector is called A, then the output vector is
A([2:4 4]) % note that there are still 4 elements here, not just 3
Walter Roberson
Walter Roberson am 8 Feb. 2024
You are copying three input locations. Adjust the assignment so that you have three output locations
attendanceValues(ThreeOutputLocations) = attendanceValues(2:4);
carefully choose ThreeOutputLocations

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by