Change value in Matlab table in multiple columns
古いコメントを表示
I have a large table and want to change the values in specific columns (columns 16:41) to a different number. So for example if the number 3 is given in one of the 26 columns, this needs to be changed to 0, a number 2 to a 1 and a number 1 to a 2. I tried to do it with a loop but it didn't work and I can write a code for each of the 26 columns separately, but there must be an easier way. I also tried what I typed below where m is my dataset, but got an error 'incorrect use of '=' operator.'
(m{:,16:41} == 3) = 0;
Anyone who knows how to do this?
回答 (1 件)
Say you matrix is A
A=[100x100];
for ii=1:length(A(:,1))
for jj=16:41
if A(ii, jj)==3, A(ii, jj)=0; end
if A(ii, jj)==1, A(ii, jj)=2; end
if A(ii, jj)==2, A(ii, jj)=1; end
end
end
4 件のコメント
This approach has a bug. Let's take a smaller A matrix and apply the three if statements to it.
ii = 1;
jj = 1;
A = 1
if A(ii, jj)==3, A(ii, jj)=0; end
if A(ii, jj)==1, A(ii, jj)=2; end
if A(ii, jj)==2, A(ii, jj)=1; end
By the description of the problem, A should be 2 after this code runs. Is it?
A
The second if statement does change the value of A from 1 to 2, but the third if statement changes it back.
Ghazwan
2022 年 10 月 11 日
You are correct Steven. I was not paying attention.
YaaW
2022 年 10 月 11 日
Ghazwan
2022 年 10 月 11 日
we would not know what the issue is without looking at the data.
カテゴリ
ヘルプ センター および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!