Column Indexing and help finishing function

조회 수: 12 (최근 30일)
Matthew Vermeer
Matthew Vermeer 2018년 2월 12일
편집: Voss 2024년 10월 30일
Hello,
So for our homework, we are told to finish function "UpdatePriceTable." As shown, I've gotten the basis of the idea but am struggling to figure out how to make the final answer output a 2 x 2 array rather than the 1 x 2 array it is outputting.
I was wondering if: A) I could get a hint for what's wrong B) I could get an extra piece of information to add to make the function the correct answer.
Here's the question;
Column array origPriceTable cotains the price per pound of various deli items. Column array changePrice indicates price adjustments for a given column. Assign newPriceTable with origPriceTable plus the newPriceTable added to origPriceTable's column colNum. Ex: If origPriceTable is [19.99, 9.99; 14.99, 8.99;], changePrice is [ -1.00; -1.50; ], and colNum is 1, then newPriceTable is [18.99, 9.99; 13.49, 8.99;].
And finally here is my current function
function newPriceTable = UpdatePriceTable( origPriceTable, changePrice, colNum )
% UpdatePriceTable: Adds changePrice to column colNum of origPriceTable
% Returns the updated price table newPriceTable
% Inputs: origPriceTable - original price data table
% changePrice - column array of pricing changes
% colNum - specified column of priceTable to update
%
% Outputs: newPriceTable - updated price data table
% Assign newPriceTable with data from priceTable;
newPriceTable1 = [origPriceTable(:,colNum) + changePrice]; % FIXME
% Assign newPriceTable column specified by colNum with original price
% data updated by changePrice
newPriceTable = [newPriceTable(:,:), origPriceTable; % FIXME
end
Thank you for any input or help!

답변 (2개)

John Snitzer
John Snitzer 2018년 10월 2일
function newPriceTable = UpdatePriceTable( origPriceTable, changePrice, colNum ) % UpdatePriceTable: Adds changePrice to column colNum of origPriceTable % Returns the updated price table newPriceTable % Inputs: origPriceTable - original price data table % changePrice - column array of pricing changes % colNum - specified column of priceTable to update % % Outputs: newPriceTable - updated price data table
% Assign newPriceTable with data from priceTable;
newPriceTable = [origPriceTable(:, colNum) + changePrice]; % FIXME
% Assign newPriceTable column specified by colNum with original price
% data updated by changePrice
origPriceTable(:,colNum) = newPriceTable;
newPriceTable = origPriceTable(:,:)
end
  댓글 수: 1
Jackson Swets
Jackson Swets 2022년 3월 14일
편집: Voss 2024년 10월 30일
good [redacted] bro

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


Jos (10584)
Jos (10584) 2018년 2월 12일
You want to update a single column, but keep the rest! You can do this by indexing on the left hand side of the assignment operator (=), as in
M(:,k) = V % change k-th column of matrix M into the vector V
Note that V should have the same number of elements of as M(:,k), or is a single scalar value, otherwise you'll get the notorious "Dimensions do not match" error.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by