how to replace the matrix by arrayfun() for the matrix like this?

1 次查看(过去 30 天)
data =
[ 1] [ 1] [ 1] [45.7600]
[ 1] [ 2] [ 2] [52.9200]
[ 2] [ 1] [ 2] [59.7600]
[ 2] [ 2] [ 1] [50.4200]
[ 98.6800] [ 105.5200] [ 96.1800] [ 0]
[110.1800] [ 103.3400] [ 112.6800] [ 0]
[ 27.8280] [ 1] [ 57.2868] [ 0]
[161.4476] [4.0522e+03] [1.6211e+04] [ 1.1881]
map =
'10 min' '2.2g' '5.0kg'
'20 min' '3.0g' '7.0kg'
Now I want to replace data{1:4,1:3} by map's content.
I mean data(2,2)=2, so it should be replaced by map(2,2)
then How should I make the code?
thank you!

采纳的回答

Stephen23
Stephen23 2015-12-7
编辑:Stephen23 2015-12-7
Your explanation is not entirely clear, and it would be useful to have a complete output example. Although you write that "I mean data(2,2)=2, so it should be replaced by map(2,2)", you do not explain why this is so: is it because they both are indexed at (2,2), or do the indices map(2,2) use the value of the element data(2,2)?
In any case it is not required to use arrayfun when indexing will do the job perfectly. Perhaps you want something like this, where I used the values of the cell array elements as indices into map:
X = { 1, 1, 1,45.7600;...
1, 2, 2,52.9200;...
2, 1, 2,59.7600;...
2, 2, 1,50.4200;...
98.6800, 105.5200, 96.1800, 0;...
110.1800, 103.3400, 112.6800, 0;...
27.8280, 1, 57.2868, 0;...
161.4476,4.0522e+03,1.6211e+04, 1.1881};
R = 1:4;
C = 1:3;
M = cell2mat(X(R,C));
map = {'10 min','2.2g','5.0kg';...
'20 min','3.0g','7.0kg'};
Z = map(M);
X(R,C) = Z;
Of course linear indexing in MATLAB is column-wise, so to access all six values of map the original cell array would need to have values up to six. Currently the cell array only has values 1 and 2, so with the given values it only accesses those two elements of map's first column.
  7 个评论
Stephen23
Stephen23 2015-12-14
编辑:Stephen23 2015-12-14
You seem to have different map values to the original question, but you might like to try this:
X = { 1, 1, 1,45.7600;...
1, 2, 2,52.9200;...
2, 1, 2,59.7600;...
2, 2, 1,50.4200;...
98.6800, 105.5200, 96.1800, 0;...
110.1800, 103.3400, 112.6800, 0;...
27.8280, 1, 57.2868, 0;...
161.4476,4.0522e+03,1.6211e+04, 1.1881};
map = {'10 min','2.2g','5.0kg';...
'20 min','3.0g','7.0kg'};
R = 1:4;
C = 1:3;
M = cell2mat(X(R,C))
N = repmat(C,max(R),1)
P = sub2ind(size(map),M,N)
Z = map(P);
X(R,C) = Z
displays this:
X =
'10 min' '2.2g' '5.0kg' [45.7600]
'10 min' '3.0g' '7.0kg' [52.9200]
'20 min' '2.2g' '7.0kg' [59.7600]
'20 min' '3.0g' '5.0kg' [50.4200]
[ 98.6800] [ 105.5200] [ 96.1800] [ 0]
[110.1800] [ 103.3400] [112.6800] [ 0]
[ 27.8280] [ 1] [ 57.2868] [ 0]
[161.4476] [4.0522e+03] [ 16211] [ 1.1881]
vx2008
vx2008 2015-12-15
编辑:vx2008 2015-12-15
Ok, This code works well.
Maybe I remember this wrongly; Thank you for your consistent supporting.
It also can be replaced by the below codes :
X(1:4,1:3)=map(bsxfun(@plus,cell2mat(X(1:4,1:3)),0:size(map,1):numel(map)-1))

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by