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?
Stephen23
2015 年 12 月 9 日
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 で Resizing and Reshaping Matrices についてさらに検索
タグ
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
