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

조회 수: 1 (최근 30일)
vx2008
vx2008 2015년 12월 7일
편집: vx2008 2015년 12월 15일
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