开发者

How to generate a matrix through a loop?

开发者 https://www.devze.com 2023-04-01 07:37 出处:网络
I´m wondering how to get these in Matlab: a = 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 开发者_JAVA技巧 Really the structure I want to do has 2 thousand files. but I will start with something ea

I´m wondering how to get these in Matlab:

a = 
1 3
2 4
3 5
4 6
5 7
6 8
7 9
8 10
9 11
10 12
开发者_JAVA技巧

Really the structure I want to do has 2 thousand files. but I will start with something easier. So I was thinking about to do it throught a loop:

for i=1:1:10
a(i) = [i i+2]
end

but this give an error:

???  In an assignment  A(I) = B, the number of elements in B and
I must be the same.

The idea is to generate a entire matrix (or structure, I suppose both are the same...) using a for loop (or may be there is a way to do it without any case of loop...). Does anyone could tell me how to do it? Thank you so much!


In your for loop, you are assigning two numbers to one element of your array a. Try

for i=1:1:10
  a(i,:) = [i i+2];
end

instead. Or, just use

a=[(1:10)' (3:12)'];

which replaces your loop...


The specific matrix, w/o loops, where n is no. of rows:

n=10;
a=(1:n)';
m=[a  a+2];
0

精彩评论

暂无评论...
验证码 换一张
取 消