# Change value in Matlab table in multiple columns

18 Ansichten (letzte 30 Tage)
YaaW am 11 Okt. 2022
Kommentiert: Ghazwan am 11 Okt. 2022
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?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Ghazwan am 11 Okt. 2022
Bearbeitet: Ghazwan am 11 Okt. 2022
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 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
YaaW am 11 Okt. 2022
Yes thanks! Figured that one out as well so changed the order of the if statements. However, it still doesn't change the values in my dataset. It gives an error for the use of 'length' for type table, but I can change this to size or height. It also gives an error for the operator '==' but I know that if I change the ( to { it works. So it doesn't give an error anymore, but also doesn't change the numbers in the dataset. Is jj defined enough? I was thinking there might be something missing to find the right columns to change the values.
Ghazwan am 11 Okt. 2022
we would not know what the issue is without looking at the data.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by