Column Indexing and help finishing function
12 views (last 30 days)
Show older comments
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!
0 Comments
Answers (2)
John Snitzer
on 2 Oct 2018
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 Comment
Jos (10584)
on 12 Feb 2018
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.
0 Comments
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!