How can I change one column of a .mat file ?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pablo Heredia
am 30 Jan. 2017
Kommentiert: Pablo Heredia
am 30 Jan. 2017
I have a .mat file which is a 1769x97 matrix of numbers. The 97th column is composed of numbers going from 1 to 100 and I need to change them to numbers going from 1 to 4. There are 1769 values (around 18 for each number of the 100) so I can't do it by hand. I need to change the values between 1 and 5, 11 and 15, 21 and 25... to 1, the values between 6 and 10, 16 and 20... to 2, the values between 51 and 55, 61 and 65... to 3 and the values between 56 and 60, 66, and 70... to 4.
What I thought of doing was this:
load matrix1769x97.mat %this creates my matrix called A in the workspace.
for i=1:1769
if A(i,97)== 1:5 || 11:15 || 21:25 || 31:35 || 41:45
A(i,97)== 1
end
if A(i,97)== 6:10 || 16:20 || 26:30 || 36:40 || 46:50
A(i,97)== 2
end
if A(i,97)== 51:55 || 61:65 || 71:75 || 81:85 || 91:95
A(i,97)== 3
end
if A(i,97)== 56:60 || 66:70 || 76:80 || 86:90 || 96:100
A(i,97)==4
end
end
It doesn't work. What changes would you do to the code? And also, can I save the new A to another .mat file after doing this? How can I do it?
Thanks in advance! :-)
0 Kommentare
Akzeptierte Antwort
Takuji Fukumoto
am 30 Jan. 2017
I think you need to write conditional statement like here
if A(i,97)== 1:5 || A(i,97)== 11:15 || A(i,97)== || 21:25 ...
I simpify the structure. you can do that with this code.
for i=1:1769
if A(i,97) <= 50
if mod(A(i,97),10) <= 5 && mod(A(i,97),10) ~= 0
A(i,97)= 1;
else
A(i,97)= 2;
end
else
if mod(A(i,97),10) <= 5 && mod(A(i,97),10) ~= 0
A(i,97)= 3;
else
A(i,97)= 4;
end
end
end
Weitere Antworten (1)
Takuji Fukumoto
am 30 Jan. 2017
You can replace data in a colum. Please see below.
imax1 = 100
imax2=4;
data = randi(imax1,[1769,97]);
newcol = randi(imax2,[1769,1]);
data(:,end) = newcol;
Siehe auch
Kategorien
Mehr zu Workspace Variables and MAT-Files 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!