Hi, can you help me fix my code? How can I convert this to a matrix using if/while/for statements?

1 回表示 (過去 30 日間)
Fortia Alfeche
Fortia Alfeche 2023 年 7 月 3 日
編集済み: dpb 2023 年 7 月 3 日
format long G
MERfgh = [];
i = 1;
%for i=1:11
MERfgh(i,1) = MERfff(i,1);
for j=1:11
MERfgh(i,2) = MERfff(j,1);
i = i+1;
end
%end
MERfgh
I WANT IT TO LOOK LIKE THIS:

回答 (2 件)

DGM
DGM 2023 年 7 月 3 日
Consider the example:
% some data in a column vector
MERfff = (21:31).';
% rearrange it
[xx yy] = meshgrid(MERfff);
MERfgh = [xx(:) yy(:)]
MERfgh = 121×2
21 21 21 22 21 23 21 24 21 25 21 26 21 27 21 28 21 29 21 30
  1 件のコメント
James Tursa
James Tursa 2023 年 7 月 3 日
Also consider this example using element-wise raise-to-power:
10.^(-1:1)
ans = 1×3
0.1000 1.0000 10.0000

サインインしてコメントする。


dpb
dpb 2023 年 7 月 3 日
編集済み: dpb 2023 年 7 月 3 日
Use the power of MATLAB; it is, after all, MATrixLABoratory...
format short e; format compact
P=2; % power range -- use small number for demo so can see output...
x=10.^[-P:P].';
M=[kron(x,ones(size(x))) repmat(x,size(x))]
M = 25×2
1.0000e-02 1.0000e-02 1.0000e-02 1.0000e-01 1.0000e-02 1.0000e+00 1.0000e-02 1.0000e+01 1.0000e-02 1.0000e+02 1.0000e-01 1.0000e-02 1.0000e-01 1.0000e-01 1.0000e-01 1.0000e+00 1.0000e-01 1.0000e+01 1.0000e-01 1.0000e+02

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by