Replace table elements in more than one row

D
Davindra Usov · Mar 29, 2023 · 2K views
Question
Hi,   I have outputted a vector (V) which contains the row indices of elements in a table (T) that contain the string 'rpm'.   T has 3 columns and I initially set all of the elements in column 3 to zero. I now wish to replace all elements in column 3 that have these row indices (so only row 3 and row 7) with a value of 6. I have tried doing this but it doesn't work:   V = [3;7]; % row indicesfor i = 1:numel(V) T(V(i), 3) = repmat(6, numel(V), 1);end   I get an error saying To assign to or create a variable in a table, the number of rows must match the height of the table.
Expert Answer
Profile picture of John Williams
John Williams PhD Expert
Answered Aug 23, 2026

You should look into how MATLAB does its indexing. You don't have to loop through the array to change a value. Just do it like this:

 

T = zeros(10,3); %i just picked 10 rows of data
indx = [3,7];
T(indx,3) = 6
T = 10×3
     0     0     0
     0     0     0
     0     0     6
     0     0     0
     0     0     0
     0     0     0
     0     0     6
     0     0     0
     0     0     0
     0     0     0

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!