Change matrix elements in a loop?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I am trying to figure out how to use a for loop to change the elements in a matrix.
I want to do this ten times.
For example, if row 1 of a 6x3 matrix is (a,a,0), I want to make a loop that generates row 2 as (2a,2a,0). I am basically trying to multiply the initial row to make all 6 rows increase by a multiple of a.
Thank you in advance for any suggestions!
採用された回答
Star Strider
2015 年 11 月 6 日
No loop necessary. Use bsxfun:
M = randi(9, 6, 3);
Mm = bsxfun(@times, M, [1:size(M,1)]')
This multiplies every row of ‘M’ by the column vector [1 2 ... 6]'.
8 件のコメント
lsutiger1
2015 年 11 月 6 日
Thank you for your response. This doesn't seem to be working, but I think that's because I left out an important piece of information as to why I need a loop: my matrix is actually 10x3x4. I have four different matrices that I need to do this to, and they all have different initial rows.
Star Strider
2015 年 11 月 6 日
My pleasure.
That definitely does change your Question. Assuming that you want to do this for every one of the four ‘pages’ of your matrix, loop through them and do the multiplication on each with the loop:
M = randi(9, 10, 3, 4); % Create Matrix
Mm = zeros(size(M)); % Preallocate
for k1 = 1:size(M,3) % Iterate Over Pages
Mp = M(:,:,k1); % Select Page
Mm(:,:,k1) = bsxfun(@times, Mp, [1:size(Mp,1)]'); % Multiply Page
end
Mp % Last Page (Optional)
Mm(:,:,k1) % Multiplied Last Page (Optional)
The two ‘Optional’ lines simply verify that the code did what we want it to. They can be deleted.
lsutiger1
2015 年 11 月 6 日
Great, that helps a lot! Thank you! If I may ask one more question.. what would I do if I had a similar 10x3x4 matrix set-up, but it was all zeroes, except for the first row of each 'page.'
For example, if row 1 was (1,0,1), what would I need to do to make row 2 (2,0,2), row 3 (3,0,3), etc.?
I tried to do the following:
if ii=1:n
M = bxsfun(@times, X(:,:,ii), [1:size(X,1)]')
end
Star Strider
2015 年 11 月 6 日
My pleasure!
It should work the same way, without modification. Anything multiplied by zero is zero, so the zeros would remain and only the non-zero values would change.
I assume you define ‘n’ in your loop as size(X,3).
lsutiger1
2015 年 11 月 6 日
The error that I am getting says:
Error using bsxfun Non-singleton dimensions of the two input arrays must match each other
Here is my entire code thus far; maybe this will help.
% Create matricies for atomic coordinates in unit cell
a = input('What is the lattice constant? \n a = ');
n = input('How many molecular orientations are required? \n n = ');
d = input('How many unit cells in each direction are desired? \n d = ');
type(:,:,n) = zeros(d*a,3);
% Generate initial molecular positions in unit cell
for ii=1:n
disp(['What is the position of molecule ' num2str(ii) ' in terms of a?']);
x = input('x-direction: ');
type(1,1,ii) = x;
y = input('y-direction: ');
type(1,2,ii) = y;
z = input('z-direction: ');
type(1,3,ii) = z;
end
% Generate lattic from initial molecular positions
for ii=1:d % create 10 unit cells in each direction
lattice = bsxfun(@times, type(:,:,ii), [1:size(type,3)]');
end
Star Strider
2015 年 11 月 6 日
You changed my code! You’re not creating the vector along the correct dimension.
Use this instead:
lattice = bsxfun(@times, type(:,:,ii), [1:size(type,1)]');
Use the row size (dimension 1) not the page size (dimension 3). That’s why it’s not working. Changing it back to my original code works.
lsutiger1
2015 年 11 月 6 日
Yes, I changed it because I was getting that error; neither the 1 or 3 seems to make a difference. This is why I am confused.
Star Strider
2015 年 11 月 6 日
I can’t run your code because I have no idea what you are doing and the inputs should be. Please attach a .mat file with the appropriate size and values for ‘type’. Use the ‘paperclip’ icon, and complete both the ‘Choose file’ and ‘Attach file’ steps.
My code does what you want it to, as you requested, but I can’t troubleshoot it with your code without the appropriate data.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!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)
