Alter number sort / number sequence
I have an array / number sequence a=[1,2,3,4,5]
and I'm trying
to create an array / number sequence that looks like a_new
below:
The columns represent the orders / index the numbers should go in.
a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]
My thoughts where to use circshift
but quickly found out that would not work.
a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end
produces
a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]
Any thoughts? It doesn't need to use circshift
if that won't work.
PS: I'm using Octave 4.2 which is similar to Matlab
matlab sequence octave
add a comment |
I have an array / number sequence a=[1,2,3,4,5]
and I'm trying
to create an array / number sequence that looks like a_new
below:
The columns represent the orders / index the numbers should go in.
a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]
My thoughts where to use circshift
but quickly found out that would not work.
a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end
produces
a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]
Any thoughts? It doesn't need to use circshift
if that won't work.
PS: I'm using Octave 4.2 which is similar to Matlab
matlab sequence octave
can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want?circshift
wont work because of this, the first column is a special case
– Ander Biguri
Nov 13 '18 at 17:11
@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 '18 at 17:17
add a comment |
I have an array / number sequence a=[1,2,3,4,5]
and I'm trying
to create an array / number sequence that looks like a_new
below:
The columns represent the orders / index the numbers should go in.
a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]
My thoughts where to use circshift
but quickly found out that would not work.
a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end
produces
a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]
Any thoughts? It doesn't need to use circshift
if that won't work.
PS: I'm using Octave 4.2 which is similar to Matlab
matlab sequence octave
I have an array / number sequence a=[1,2,3,4,5]
and I'm trying
to create an array / number sequence that looks like a_new
below:
The columns represent the orders / index the numbers should go in.
a_new=...
[1,2,3,4,5;
2,1,2,3,4;
3,3,1,2,3;
4,4,4,1,2;
5,5,5,5,1]
My thoughts where to use circshift
but quickly found out that would not work.
a=[1,2,3,4,5];
for n=1:5
a_wrong(:,n)=circshift(a(:)',[0 n])(:)
end
produces
a_wrong=[
5 4 3 2 1
1 5 4 3 2
2 1 5 4 3
3 2 1 5 4
4 3 2 1 5]
Any thoughts? It doesn't need to use circshift
if that won't work.
PS: I'm using Octave 4.2 which is similar to Matlab
matlab sequence octave
matlab sequence octave
edited Nov 13 '18 at 17:28
Cris Luengo
19.8k52249
19.8k52249
asked Nov 13 '18 at 17:00
Rick TRick T
1,52042973
1,52042973
can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want?circshift
wont work because of this, the first column is a special case
– Ander Biguri
Nov 13 '18 at 17:11
@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 '18 at 17:17
add a comment |
can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want?circshift
wont work because of this, the first column is a special case
– Ander Biguri
Nov 13 '18 at 17:11
@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 '18 at 17:17
can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want?
circshift
wont work because of this, the first column is a special case– Ander Biguri
Nov 13 '18 at 17:11
can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want?
circshift
wont work because of this, the first column is a special case– Ander Biguri
Nov 13 '18 at 17:11
@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 '18 at 17:17
@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 '18 at 17:17
add a comment |
3 Answers
3
active
oldest
votes
There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat
, toeplitz
, tril
, and triu
:
>> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))
a_new =
1 2 3 4 5
2 1 2 3 4
3 3 1 2 3
4 4 4 1 2
5 5 5 5 1
add a comment |
I'm not sure about a built-in function, but this should work;
a=[1,2,3,4,5];
a_out = ones(length(a), length(a))
for n=1:5
a_out(n,:) = [n*ones(n-1),a(n:end)]
end
I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!
add a comment |
You can use spdiags
to generate the matrix:
n = numel(a);
a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286059%2falter-number-sort-number-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat
, toeplitz
, tril
, and triu
:
>> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))
a_new =
1 2 3 4 5
2 1 2 3 4
3 3 1 2 3
4 4 4 1 2
5 5 5 5 1
add a comment |
There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat
, toeplitz
, tril
, and triu
:
>> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))
a_new =
1 2 3 4 5
2 1 2 3 4
3 3 1 2 3
4 4 4 1 2
5 5 5 5 1
add a comment |
There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat
, toeplitz
, tril
, and triu
:
>> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))
a_new =
1 2 3 4 5
2 1 2 3 4
3 3 1 2 3
4 4 4 1 2
5 5 5 5 1
There are probably quite a few different ways to generate this matrix. Here's one using the functions repmat
, toeplitz
, tril
, and triu
:
>> a_new = tril(repmat(a.', 1, numel(a)), -1)+triu(toeplitz(a))
a_new =
1 2 3 4 5
2 1 2 3 4
3 3 1 2 3
4 4 4 1 2
5 5 5 5 1
edited Nov 13 '18 at 17:18
answered Nov 13 '18 at 17:11
gnovicegnovice
116k13231334
116k13231334
add a comment |
add a comment |
I'm not sure about a built-in function, but this should work;
a=[1,2,3,4,5];
a_out = ones(length(a), length(a))
for n=1:5
a_out(n,:) = [n*ones(n-1),a(n:end)]
end
I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!
add a comment |
I'm not sure about a built-in function, but this should work;
a=[1,2,3,4,5];
a_out = ones(length(a), length(a))
for n=1:5
a_out(n,:) = [n*ones(n-1),a(n:end)]
end
I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!
add a comment |
I'm not sure about a built-in function, but this should work;
a=[1,2,3,4,5];
a_out = ones(length(a), length(a))
for n=1:5
a_out(n,:) = [n*ones(n-1),a(n:end)]
end
I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!
I'm not sure about a built-in function, but this should work;
a=[1,2,3,4,5];
a_out = ones(length(a), length(a))
for n=1:5
a_out(n,:) = [n*ones(n-1),a(n:end)]
end
I do not have Octave or MATLAB installed on my computer, so I cannot test it. This may have a silly error, forgive me for that!
answered Nov 13 '18 at 17:19
SRT HellKittySRT HellKitty
29318
29318
add a comment |
add a comment |
You can use spdiags
to generate the matrix:
n = numel(a);
a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);
add a comment |
You can use spdiags
to generate the matrix:
n = numel(a);
a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);
add a comment |
You can use spdiags
to generate the matrix:
n = numel(a);
a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);
You can use spdiags
to generate the matrix:
n = numel(a);
a_new = spdiags([repmat(flip(a).',1,n); repmat(a,n-1,1)],(1-n):0);
answered Nov 14 '18 at 4:04
rahnema1rahnema1
10.3k2923
10.3k2923
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286059%2falter-number-sort-number-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you explain your matrix? why is the first column a different direction than the others? Are you 100% sure this is what you want?
circshift
wont work because of this, the first column is a special case– Ander Biguri
Nov 13 '18 at 17:11
@AnderBiguri the columns represent the orders / index the numbers should go in.
– Rick T
Nov 13 '18 at 17:17