how can i get a matrix from an array like this:

Starting with
a = [12 21 32];
I would like to get this:
M = [12 21 32; 21 32 12; 32 12 21];
with size and length fun. only
Is it some kind of loop???

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 2 日
編集済み: Azzi Abdelmalek 2013 年 1 月 2 日

0 投票

a = [12 21 32 42];
n=length(a)
M=a
for k=1:n-1
c=a(1)
a(1:end-1)=a(2:end)
a(end)=c
M=[M;a]
end

2 件のコメント

Zaza
Zaza 2013 年 1 月 2 日
thanks a lot Azzi
ןt was a bit tricky for me
Walter Roberson
Walter Roberson 2013 年 1 月 2 日
This does not satisfy your stated requirements, as assignment and subscripting and [] are all functions in MATLAB: in particular, those operations call upon subsasgn(), subsref(), and vertcat (and horzcat).

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

その他の回答 (5 件)

Roger Stafford
Roger Stafford 2013 年 1 月 2 日

1 投票

M = hankel(a,fliplr(a));
or if you can't use the 'hankel' function do this
n = length(a);
M = reshape(a(mod(floor(((n+1)*(0:n^2-1))/n),n)+1),n,n);
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 2 日
編集済み: Azzi Abdelmalek 2013 年 1 月 2 日

0 投票

M=[a ;circshift(a,[0 -1]); circshift(a,[0 -2])]
%or more general
a=[12 21 32 42 52]
M=cell2mat(arrayfun(@(x) circshift(a,[0 -x]),(0:numel(a)-1)','un',0))

6 件のコメント

Zaza
Zaza 2013 年 1 月 2 日
編集済み: Zaza 2013 年 1 月 2 日
can i get this using for or while? because in HW i can use length and size fun. only
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 2 日
編集済み: Azzi Abdelmalek 2013 年 1 月 2 日
Yes you can, try for loop
Zaza
Zaza 2013 年 1 月 2 日
for i=1:length(a)
M(i,:) = a(i);
i get the 1st column of M but i can't manipulate M to get the correct form of M...
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 2 日
Use circshift function in the loop
Zaza
Zaza 2013 年 1 月 2 日
i can't use this fun.
but i can't do it with size and length only
oh i must take a deep breath and try later...
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 2 日
I will give you a hint
a=[12 21 32]
b=a([2 3 1])

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

Walter Roberson
Walter Roberson 2013 年 1 月 2 日

0 投票

You can look at the code for toeplitz.m
Image Analyst
Image Analyst 2013 年 1 月 2 日

0 投票

I don't know what "with size and length fun. only" means, but the simplest solution is
M = [a(1,1), a(1,2), a(1,3); a(1,2), a(1,3), a(1,1); a(1,3), a(1,1), a(1,2)];
Since you haven't given any other requirements, like it needs to work for other sizes of matrices, this will work exactly as you have said. No functions like toeplitz() or circshift() needed. If you needed other requirements and flexibility, I assume you would have said so (ok, that's probably a bad assumption given the types of posts people make).

4 件のコメント

Zaza
Zaza 2013 年 1 月 2 日
i meant that i can use only "size" and "length" functions
and i forget to mention that this has to work on any size of given array...
Image Analyst
Image Analyst 2013 年 1 月 2 日
編集済み: Image Analyst 2013 年 1 月 2 日
Did you also forget to mention that it is homework? Because I can think of no other reason why you would be prohibited from using any function at your disposal to get the job done. Add that tag to your post if it's homework. We won't just give you the answer to your homework outright, but may give hints.
Zaza
Zaza 2013 年 1 月 2 日
i did mention that
tips are welcome
Image Analyst
Image Analyst 2013 年 1 月 2 日
編集済み: Image Analyst 2013 年 1 月 2 日
OK - I didn't know the acronym HW. Others probably didn't recognize that as homework either, because it looks like three people have already done your homework for you and given you an answer that should work. I'll add the "homework" tag for you. Next time you can add it yourself so you won't be caught copying.

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

Sean de Wolski
Sean de Wolski 2013 年 1 月 2 日

0 投票

M = a(sortrows(bsxfun(@(x,y)mod(x+y,numel(a))+1,1:numel(a),(1:numel(a))')))

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by