Extract columns and rows from matrix
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I need to extract a column (let's say 1st) from 1st matrix (5x5) to use it as a row (let's say 5th) in a 2nd matrix (5x5).
Thanks in advance
採用された回答
first = rand(5)
first = 5×5
0.6556 0.1094 0.2522 0.5119 0.5134
0.4183 0.3149 0.5010 0.4793 0.0783
0.9219 0.1391 0.7669 0.4079 0.2564
0.3306 0.2384 0.4618 0.3429 0.0067
0.3400 0.3090 0.4482 0.6066 0.5850
second = rand(5)
second = 5×5
0.7720 0.9819 0.2196 0.4291 0.6845
0.8561 0.0148 0.9716 0.8685 0.8155
0.3618 0.2091 0.7744 0.6708 0.5010
0.1139 0.4576 0.3263 0.4784 0.1249
0.3806 0.5838 0.4502 0.5900 0.5781
second(5,:) = first(:,1).'
second = 5×5
0.7720 0.9819 0.2196 0.4291 0.6845
0.8561 0.0148 0.9716 0.8685 0.8155
0.3618 0.2091 0.7744 0.6708 0.5010
0.1139 0.4576 0.3263 0.4784 0.1249
0.6556 0.4183 0.9219 0.3306 0.3400
7 件のコメント
Heorhii
2023 年 11 月 4 日
Much appreciated!!
Stephen23
2023 年 11 月 4 日
Note that the TRANSPOSE is optional:
first = rand(5)
first = 5×5
0.2622 0.9271 0.8045 0.4209 0.9297
0.1965 0.1722 0.4752 0.5092 0.2747
0.5255 0.8954 0.8495 0.5751 0.1948
0.5142 0.3306 0.3673 0.1700 0.1440
0.1852 0.6967 0.2868 0.3201 0.3975
second = rand(5)
second = 5×5
0.0998 0.1903 0.2646 0.7202 0.6271
0.3664 0.1022 0.9504 0.5349 0.1289
0.7642 0.0478 0.2630 0.9633 0.7535
0.5964 0.9338 0.9450 0.4532 0.1578
0.8699 0.7766 0.4579 0.5648 0.2570
second(5,:) = first(:,1)
second = 5×5
0.0998 0.1903 0.2646 0.7202 0.6271
0.3664 0.1022 0.9504 0.5349 0.1289
0.7642 0.0478 0.2630 0.9633 0.7535
0.5964 0.9338 0.9450 0.4532 0.1578
0.2622 0.1965 0.5255 0.5142 0.1852
Torsten
2023 年 11 月 4 日
Interesting that MATLAB doesn't differ between row and column in this case. Why is it ?
madhan ravi
2023 年 11 月 5 日
@Torsten that is because the number of elements in L.H.S is equal to the R.H.S
Dyuman Joshi
2023 年 11 月 5 日
@madhan ravi, then this should work as well, but it doesn't.
y = rand(5,5)
y = 5×5
0.2054 0.3064 0.3352 0.1937 0.8819
0.9259 0.3211 0.8781 0.5697 0.8578
0.3453 0.2379 0.6059 0.2627 0.2939
0.5965 0.9375 0.1124 0.3127 0.4328
0.2790 0.7215 0.9603 0.4169 0.3628
y([2 3], :) = y(:, [2 3])
Unable to perform assignment because the size of the left side is 2-by-5 and the size of the right side is 5-by-2.
Dyuman Joshi
2023 年 11 月 5 日
For multidimensional arrays, A(i,j,k,…) = B assigns B to the specified elements of A. B must be length(i)-by-length(j)-by-length(k)-… or be shiftable to that size by adding or removing singleton dimensions.
1xN is shiftable to Nx1 by removing the 1st singleton dimesion.
1xN == 1xNx1 -> Nx1
Torsten
2023 年 11 月 5 日
Thank you for the explanation.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Multidimensional Arrays についてさらに検索
製品
タグ
参考
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)
