フィルターのクリア

How to create an mxn matrix with a for-loop

129 ビュー (過去 30 日間)
Dominique Brasee
Dominique Brasee 2016 年 7 月 28 日
コメント済み: Rik 2020 年 11 月 6 日
Hello all,
I would like to display (x(i),y(j)) in separate columns according to the x(i)'s.
i.e.:
(0,0) (1,0) (2,0)
(0,1) (1,1) (2,1)
(0,2) (1,2) (2,2), etc.
I am currently using a nested for-loop to calculate all (x(i),y(j)) as follows:
for i = 1:3
a = i-1;
for j = 1:3
b = j-1;
X = [a b];
disp(X);
end
end
I get the output as follows:
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
I tried using reshape to display the output as above, however in fiddling with the program, I discovered that MATLAB considers each line of output to be a separate 1x2 matrix. Do you have any suggestions as to how to get MATLAB to recognize the outputs as a 9x2 matrix? This is an extremely scaled-down version of what I need the program to do.
Thank you!!
  4 件のコメント
ABISHAI JOY
ABISHAI JOY 2020 年 2 月 16 日
How will you put cos (x+y) in to a matrix and it should generate random values with x, y between [0,2pi].
dpb
dpb 2020 年 2 月 16 日
Use rand(), maybe???

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

採用された回答

dpb
dpb 2016 年 7 月 28 日
As you've written it, you only have a 1x2 (row) vector at any one time, you overwrite X on each pass. There are certainly more efficient ways to generate this, but the issue with respect to building an array is simple to handle; I presume there's something much more than this going on in the real problem...
N=3;
k=0;
for i=0:N-1
for j=0:N-1
k=k+1;
X(k,:) = [i j];
end
end
disp(X)
  2 件のコメント
Dominique Brasee
Dominique Brasee 2016 年 7 月 28 日
In the real program I will be computing a very large set of coordinates but I am trying to get a feel for a generic method (work asked me to do some modeling to fix some bugs in an existing program and I want to make sure I don't look like a complete idiot, since I am the intern haha).
Thank you for your help!
dpb
dpb 2016 年 7 月 28 日
You recognize that looping isn't very Matlab-like here, I presume?
>> [reshape(repmat(0:N-1,N,1),[],1) repmat(0:N-1,1,N).']
ans =
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
>>

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

その他の回答 (3 件)

Lam Nguyen Van
Lam Nguyen Van 2020 年 3 月 10 日
Hi,
I want to creat matrices automatically with for loop in Matlab?
phi1=30; phi2=45; phi3=90;
After running the code I want to have a matric A with the following elements:
A=[sin(phi1) cos(phi1) sin(phi1)*cos(phi1);
sin(phi2) cos(phi2) sin(phi2)*cos(phi2);
sin(phi3) cos(phi3) sin(phi3)*cos(phi3);]
  1 件のコメント
Rik
Rik 2020 年 11 月 6 日
Your answers look like questions. Please post them as questions. Have a read here and here. It will greatly improve your chances of getting an answer.

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


Lam Nguyen Van
Lam Nguyen Van 2020 年 4 月 16 日
https://www.mathworks.com/matlabcentral/answers/372036-can-you-create-a-matrix-using-a-for-loop?s_tid=mlc_ans_email_view&utm_source=zalo&utm_medium=zalo&utm_campaign=zalo&zarsrc=1303

Lam Nguyen Van
Lam Nguyen Van 2020 年 4 月 17 日
clear;
phi=[30, 45, 90];
n=length(phi);
A=zeros(n,3);
for i=1:n
[d]= ham_luong_giac (phi(i));
A(i,:)=[d];
end
A
function [xuat] = ham_luong_giac (phi)
a1=sin(phi);
a2=cos(phi);
a3=sin(phi).*cos(phi);
xuat= [a1, a2, a3];
end

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by