Arithmetic array operations Add adjustVal to each element of array originalReadings.

Arithmetic array operations Add adjustVal to each element of array originalReadings. Your Function Save C Reset MATLAB Documentationfunction modifiedReadings = CalibrateReadings(originalReadings, adjustVal) % originalReadings: Array of temperature readings % adjustVal: value added to each element in the array of temperature readings % Add adjustVal to each element of array originalReadings end

Code to call your function C ResetCalibrateReadings([51, 53, 61, 62], 1)

Indexing an array element Assign studentScore with the second element of array classScores. Your Function Save C Reset MATLAB Documentationfunction studentScore = GetScore(classScores) % classScores: Array of test scores % Assign studentScore with the second element of array classScores end

Code to call your function C ResetGetScore([99, 85, 71])

Indexing the array: Moving values Write three statements to shift the SampleReadings contents 1 position to the left. The rightmost element after shifting should be assigned -1 Ex: If SampleReadings is [12, 85, 43], then after shifting SampleReadings becomes [85, 43, -1]. Your Function Save C Reset MATLAB Documentationfunction sampleReadings = ShiftValues(sampleReadings) % sampleReadings: Array containing 3 elements % Write three statements to shift the sampleReadings array contents 1 position to the left % Note: The rightmost element should be -1 end

Code to call your function C ResetShiftValues([12, 85, 43])

The Correct Answer and Explanation is:


1. Add adjustVal to each element of originalReadings:

function modifiedReadings = CalibrateReadings(originalReadings, adjustVal)
    % Add adjustVal to each element of originalReadings
    modifiedReadings = originalReadings + adjustVal;
end

Call Example:

CalibrateReadings([51, 53, 61, 62], 1)

2. Assign studentScore with the second element of classScores:

function studentScore = GetScore(classScores)
    % Assign studentScore with the second element
    studentScore = classScores(2);
end

Call Example:

GetScore([99, 85, 71])

3. Shift array left and assign -1 to last element:

function sampleReadings = ShiftValues(sampleReadings)
    % Shift values to the left
    sampleReadings(1) = sampleReadings(2);
    sampleReadings(2) = sampleReadings(3);
    sampleReadings(3) = -1;
end

Call Example:

ShiftValues([12, 85, 43])

Explanation

These MATLAB functions demonstrate fundamental array operations, essential for processing and transforming data efficiently.

In the first function (CalibrateReadings), we use vectorized operations, a core MATLAB feature, to add adjustVal to each element in originalReadings. MATLAB allows direct arithmetic between arrays and scalars. This eliminates the need for loops and enhances performance and readability. For instance, adding 1 to [51, 53, 61, 62] returns [52, 54, 62, 63].

The second function (GetScore) introduces array indexing, which is crucial for accessing specific elements. MATLAB arrays are 1-based, meaning the first element is indexed as 1. By accessing classScores(2), we directly retrieve the second test score in the array, for example, 85 from [99, 85, 71].

The third function (ShiftValues) demonstrates manual element reassignment to simulate a left-shift. Since MATLAB doesn’t have a built-in left shift for basic arrays, each element is reassigned explicitly. sampleReadings(1) gets the value of the second element, sampleReadings(2) takes the third, and the third is set to -1, completing the shift. This simulates array manipulation often needed in simulations or embedded systems.

Overall, these exercises develop understanding of vector operations, indexing, and array manipulation — foundational skills for MATLAB programming in engineering, data analysis, and automation contexts.

Scroll to Top