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

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!

 Akzeptierte Antwort

Stephen23
Stephen23 am 7 Dez. 2015
Bearbeitet: Stephen23 am 7 Dez. 2015
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 Kommentare

vx2008
vx2008 am 8 Dez. 2015
Bearbeitet: Guillaume am 9 Dez. 2015
Wonderfull!Thanks!
But I find something strange:
in the fact I also can do the same work by the below code:
for i=1:3
for j=1:4
data{j,i}=str{r+1+data{j,i},i+1};
end
end
and when I test the two pieces of codes by tic and toc I find that your code need time: Elapsed time is 0.001237 seconds.
and 'for' cycle need time:Elapsed time is 0.000992 seconds.
I think your code should be more quickly because matlab is not good at 'for' cycle, so why? or the calculation is too small?
The size is a bit small to be meaningful. Try magnitude increases in the matrix size (from one to one million elements), repeating each algorithm one thousand times, and then plot the required time for each of these. That will give you a better idea of how fast the operations are.
vx2008
vx2008 am 14 Dez. 2015
Bearbeitet: Stephen23 am 14 Dez. 2015
Thank you for all the replies!
But just now it's surprising for me to find that:
I test your codes before on my matlab many days ago and it worked well; But just now I test it on my matlab again and get the result as below: (something goes wrong?)
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;
X =
'10 min' '10 min' '10 min' [45.7600]
'10 min' '20 min' '20 min' [52.9200]
'20 min' '10 min' '20 min' [59.7600]
'20 min' '20 min' '10 min' [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]
Stephen23
Stephen23 am 14 Dez. 2015
Bearbeitet: Stephen23 am 14 Dez. 2015
This is exactly the same result as my original answer gives, which you accepted.
I have no idea if something is going wrong, because you still have not explained to us what going right means. Sorry, but my mind-reading skills are a bit rusty, and I have no idea what the right output should be. If you explained how the output should look like and why it would be like that, then a useful answer is more likely.
Showing us a wrong output does not explain what the right output should be.
'30 min' '1.5 g' '45℃' [53.1100]
'30 min' '2.0 g' '55℃' [59.0900]
'60 min' '1.5 g' '55℃' [71.3100]
'60 min' '2.0 g' '45℃' [70.2400]
[112.2000] [ 124.4200] [ 123.3500] [ 0]
[141.5500] [ 129.3300] [ 130.4000] [ 0]
[ 35.7317] [ 1] [ 2.0617] [ 0]
[161.4476] [4.0522e+03] [1.6211e+04] [ 6.0270]
should be as above.
Stephen23
Stephen23 am 14 Dez. 2015
Bearbeitet: Stephen23 am 14 Dez. 2015
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 am 15 Dez. 2015
Bearbeitet: vx2008 am 15 Dez. 2015
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))

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Tags

Gefragt:

am 7 Dez. 2015

Bearbeitet:

am 15 Dez. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by