Replace table elements in more than one row

Illustration
Davindra Usov - 2023-03-29T13:11:20+00:00
Question: Replace table elements in more than one row

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 answered . 2025-11-20

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

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!