Replace entries only in specific column
8 ビュー (過去 30 日間)
古いコメントを表示
Hi I have a mat file which has 20 values in its first column I only want to replace entries ONLY in first column
So my uniques values in my first columns is 1,2,3,4,5,6,7,8,9,11,23,34,44,55,56,12,24,29,30
so i want to replace all first column entries where current value is either of 1,2,3,4,5 replace them to 1001 where current vlue is 8,9,11,24 and replace that to 1002 where current vlue is 23,44,55 and replace that to 1003
How can I do this ?
Any suggestions.
I can export to excel but is there a way i can do in mat file only?
0 件のコメント
回答 (2 件)
dpb
2016 年 10 月 16 日
編集済み: dpb
2016 年 10 月 17 日
"_...can do in mat file only?"_
Sure, see
doc matfile % for all the details
Although unless the file is really big, is probably faster to read the array into memory via load, make the changes in memory and then rewrite with save
But, since you asked... :)
mat=matfile('yourMATfilename','Writable',True); % get matfile object handle
details=whos(mat); % find the skinny on the .mat file object
vname=details.name; % the variable name (if not already known)
vsize=details.size; % and its size
data=mat.(vname)(1:vsize(1),1); % load first column into workspace
ix={[1:5]; [8,9,11,24]; [23,44,55]}; % values to modify
vx=[1001:1003].'; % values to substitute for each group of ix
for i=1:length(ix)
data(ismember(data,ix{i}))=vx(i); % substitute where ix is found with vx
end
mat.(vname)(1:vsize(1),1)=data; % and save the updated column to file
clear mat % close the object
2 件のコメント
dpb
2016 年 10 月 17 日
Yeah, I started out with an index then decided to just store the result directly and then didn't remove the index variable...just delete the is=; the assignment is to the data array. Fixed up the Answer to match...
Austin
2016 年 10 月 16 日
Yes. You could just change the data directly in your where you have it stored as an array, but this can be a hassle if it is a big array.
You can index into the array and change specific elements:
data(2,1)=1001% this replaces the value in the 2nd row, first column with 1001
To do a bunch at once:
data=[1:1:15]';%data is one column.
a=data<=5 & data>=1%this finds the values that are less then or equal to 5 and greater than or equal to 1
data(a)=1001% this swaps all the values a was true for with the value 1001
I would just define the first column as a variable, fix the elements, then replace the first column with the fixed variable; but I think there should be a better way to do this.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!