How can I change one column of a .mat file ?

조회 수: 3 (최근 30일)
Pablo Heredia
Pablo Heredia 2017년 1월 30일
댓글: Pablo Heredia 2017년 1월 30일
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! :-)

채택된 답변

Takuji Fukumoto
Takuji Fukumoto 2017년 1월 30일
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

추가 답변 (1개)

Takuji Fukumoto
Takuji Fukumoto 2017년 1월 30일
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;
  댓글 수: 1
Pablo Heredia
Pablo Heredia 2017년 1월 30일
Thank you for the answer Takeru. However, it doesn't solve my problem. I realised my question wasn't complete, so I have specified a little bit more what I need to do.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by