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!
채택된 답변
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
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.
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]
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.
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]
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개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
태그
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
