repeat loop in a table

1 次查看(过去 30 天)
Matt Brianik
Matt Brianik 2018-1-16
回答: Ronit 2024-8-21
How do i make it so that the code below will replace T(i)==5 with Y(i) in multiple columns of table T. I can get it to work well when there is only one columns but have trouble making it repeat in additional columns
clear
X=(1:12);
Y=(101:112);
Table = [X;Y;Y;Y;X]
T=X.'
T=[T;T]
i=1
for n=1:12
if T(i)==5
T(i)=Y(i)
else
T(i)=T(i)
i=i+1
end
end

回答(1 个)

Ronit
Ronit 2024-8-21
Hello Matt,
To replace values in multiple columns of a table in MATLAB, you can loop through each column and apply the same logic.
% Convert the matrix to a table
T = array2table(T);
% Loop through each column of the table
for col = 1:width(T)
for i = 1:height(T)
if T{i, col} == 5
T{i, col} = Y(mod(i,width(X)));
end
end
end
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by