开发者

how do I put the results from an iteration into an array in matlab

a=[1 2 3 4
   5 6 7 8
   8 7 6 5
   4 3 2 1]

for i=(1:4)
b=(a(i,:));
c=sort(b,2)
end

Please, How can I obtain the results from this iteration in a single array(4x4) instead开发者_StackOverflow中文版 of getting the results of c=sort(b,2) separately for each loop.


You don't have to use a loop at all! You're trying to sort the columns in each row. This can be achieved by supplying an optional argument to sort.

c=sort(a,2);
c=
     1     2     3     4
     5     6     7     8
     5     6     7     8
     1     2     3     4

should give you what you need. The argument 2 tells sort to sort a by columns. If you wanted to sort it by rows, you'd use c=sort(a,1)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜