Stretch or resample 1D array





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















This question might be something really simple and I might be missing something really basic, but how do you interpolate a 1D array in C#?



Lets say I have this array of n elements



int array1 = new int { 1, 3, 5, 7, 1 };


How to stretch or compress the array so that it has n values and interpolates the values, just like when you resize an image, thats it, not chopping or adding zeros or empty values to the array.



For example if I want to convert the array so it has n = 4 elements, get this



array1
>>[2, 4, 6, 4]


Strech example



what I'm trying to do is the same as the resample function from matlab does
https://mathworks.com/help/signal/ref/resample.html










share|improve this question

























  • I don't understand how you got from the first array to the second one. Did you take the first n elements and add 1 to them? Something else?

    – mjwills
    Nov 17 '18 at 7:14













  • What is the calculation you are looking for? What is stretch supose to do?

    – Aldert
    Nov 17 '18 at 7:15











  • @mjwills It seems to be an average of 1 and 3, 3 and 5, etc.

    – John
    Nov 17 '18 at 7:15






  • 4





    Please show us at least 7 other sample inputs and the expected results for those inputs. Ensure that some of the inputs don't have numbers that are exactly 2 apart from each other. Also explain what n is for.

    – mjwills
    Nov 17 '18 at 7:23






  • 1





    There's a C++ version here if it helps.

    – John
    Nov 17 '18 at 9:22


















-2















This question might be something really simple and I might be missing something really basic, but how do you interpolate a 1D array in C#?



Lets say I have this array of n elements



int array1 = new int { 1, 3, 5, 7, 1 };


How to stretch or compress the array so that it has n values and interpolates the values, just like when you resize an image, thats it, not chopping or adding zeros or empty values to the array.



For example if I want to convert the array so it has n = 4 elements, get this



array1
>>[2, 4, 6, 4]


Strech example



what I'm trying to do is the same as the resample function from matlab does
https://mathworks.com/help/signal/ref/resample.html










share|improve this question

























  • I don't understand how you got from the first array to the second one. Did you take the first n elements and add 1 to them? Something else?

    – mjwills
    Nov 17 '18 at 7:14













  • What is the calculation you are looking for? What is stretch supose to do?

    – Aldert
    Nov 17 '18 at 7:15











  • @mjwills It seems to be an average of 1 and 3, 3 and 5, etc.

    – John
    Nov 17 '18 at 7:15






  • 4





    Please show us at least 7 other sample inputs and the expected results for those inputs. Ensure that some of the inputs don't have numbers that are exactly 2 apart from each other. Also explain what n is for.

    – mjwills
    Nov 17 '18 at 7:23






  • 1





    There's a C++ version here if it helps.

    – John
    Nov 17 '18 at 9:22














-2












-2








-2


1






This question might be something really simple and I might be missing something really basic, but how do you interpolate a 1D array in C#?



Lets say I have this array of n elements



int array1 = new int { 1, 3, 5, 7, 1 };


How to stretch or compress the array so that it has n values and interpolates the values, just like when you resize an image, thats it, not chopping or adding zeros or empty values to the array.



For example if I want to convert the array so it has n = 4 elements, get this



array1
>>[2, 4, 6, 4]


Strech example



what I'm trying to do is the same as the resample function from matlab does
https://mathworks.com/help/signal/ref/resample.html










share|improve this question
















This question might be something really simple and I might be missing something really basic, but how do you interpolate a 1D array in C#?



Lets say I have this array of n elements



int array1 = new int { 1, 3, 5, 7, 1 };


How to stretch or compress the array so that it has n values and interpolates the values, just like when you resize an image, thats it, not chopping or adding zeros or empty values to the array.



For example if I want to convert the array so it has n = 4 elements, get this



array1
>>[2, 4, 6, 4]


Strech example



what I'm trying to do is the same as the resample function from matlab does
https://mathworks.com/help/signal/ref/resample.html







c# arrays int






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 9:13







Boris

















asked Nov 17 '18 at 7:06









BorisBoris

166




166













  • I don't understand how you got from the first array to the second one. Did you take the first n elements and add 1 to them? Something else?

    – mjwills
    Nov 17 '18 at 7:14













  • What is the calculation you are looking for? What is stretch supose to do?

    – Aldert
    Nov 17 '18 at 7:15











  • @mjwills It seems to be an average of 1 and 3, 3 and 5, etc.

    – John
    Nov 17 '18 at 7:15






  • 4





    Please show us at least 7 other sample inputs and the expected results for those inputs. Ensure that some of the inputs don't have numbers that are exactly 2 apart from each other. Also explain what n is for.

    – mjwills
    Nov 17 '18 at 7:23






  • 1





    There's a C++ version here if it helps.

    – John
    Nov 17 '18 at 9:22



















  • I don't understand how you got from the first array to the second one. Did you take the first n elements and add 1 to them? Something else?

    – mjwills
    Nov 17 '18 at 7:14













  • What is the calculation you are looking for? What is stretch supose to do?

    – Aldert
    Nov 17 '18 at 7:15











  • @mjwills It seems to be an average of 1 and 3, 3 and 5, etc.

    – John
    Nov 17 '18 at 7:15






  • 4





    Please show us at least 7 other sample inputs and the expected results for those inputs. Ensure that some of the inputs don't have numbers that are exactly 2 apart from each other. Also explain what n is for.

    – mjwills
    Nov 17 '18 at 7:23






  • 1





    There's a C++ version here if it helps.

    – John
    Nov 17 '18 at 9:22

















I don't understand how you got from the first array to the second one. Did you take the first n elements and add 1 to them? Something else?

– mjwills
Nov 17 '18 at 7:14







I don't understand how you got from the first array to the second one. Did you take the first n elements and add 1 to them? Something else?

– mjwills
Nov 17 '18 at 7:14















What is the calculation you are looking for? What is stretch supose to do?

– Aldert
Nov 17 '18 at 7:15





What is the calculation you are looking for? What is stretch supose to do?

– Aldert
Nov 17 '18 at 7:15













@mjwills It seems to be an average of 1 and 3, 3 and 5, etc.

– John
Nov 17 '18 at 7:15





@mjwills It seems to be an average of 1 and 3, 3 and 5, etc.

– John
Nov 17 '18 at 7:15




4




4





Please show us at least 7 other sample inputs and the expected results for those inputs. Ensure that some of the inputs don't have numbers that are exactly 2 apart from each other. Also explain what n is for.

– mjwills
Nov 17 '18 at 7:23





Please show us at least 7 other sample inputs and the expected results for those inputs. Ensure that some of the inputs don't have numbers that are exactly 2 apart from each other. Also explain what n is for.

– mjwills
Nov 17 '18 at 7:23




1




1





There's a C++ version here if it helps.

– John
Nov 17 '18 at 9:22





There's a C++ version here if it helps.

– John
Nov 17 '18 at 9:22












1 Answer
1






active

oldest

votes


















2














I suggest this solution for the case that the new array is shorter than the old one:



int array1 = new int { 1, 3, 5, 7, 9 };
int array2 = new int[4];

for (var i = 0; i < array2.Length; i++)
{
var doubleIndex1 = (double)i * array1.Length / array2.Length;
var index1 = (int)Math.Floor(doubleIndex1);
var rel = doubleIndex1 - index1;

array2[i] = (int)Math.Round((1.0 - rel) * array1[index1] + rel * array1[index1 + 1]);
}





share|improve this answer
























  • yeah, it works that way but only when the values in the array are linearly growing or decreasing, thanks anyway

    – Boris
    Nov 17 '18 at 7:47













  • Well yes, this is linear interpolation. Do you need someting more sophisticated? Then have a look at en.wikipedia.org/wiki/Interpolation

    – Klaus Gütter
    Nov 17 '18 at 8:11






  • 2





    @Boris that's what you get for not providing a good set of examples when asked to.

    – John
    Nov 17 '18 at 8:12











  • just a resize dude, as when you stretch or shrink an image, I really dont get whats so hard to understand, I feel like I'm speaking a totally different language from anyone else, dont know how else to explain it, sorry

    – Boris
    Nov 17 '18 at 8:48






  • 1





    @Boris as a programmer and an amateur photographer, I'll tell you that when it comes to resizing an image Photoshop gives me a lot of resampling options, including: bicubic, nearest neighbour, and bilinear. There are a vast number of interpolation algorithms, etc. With a single example, it's impossible to tell which one you want. Klaus believed that linear was the method you wanted, now you're saying it's not.

    – John
    Nov 17 '18 at 9:08














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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349037%2fstretch-or-resample-1d-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I suggest this solution for the case that the new array is shorter than the old one:



int array1 = new int { 1, 3, 5, 7, 9 };
int array2 = new int[4];

for (var i = 0; i < array2.Length; i++)
{
var doubleIndex1 = (double)i * array1.Length / array2.Length;
var index1 = (int)Math.Floor(doubleIndex1);
var rel = doubleIndex1 - index1;

array2[i] = (int)Math.Round((1.0 - rel) * array1[index1] + rel * array1[index1 + 1]);
}





share|improve this answer
























  • yeah, it works that way but only when the values in the array are linearly growing or decreasing, thanks anyway

    – Boris
    Nov 17 '18 at 7:47













  • Well yes, this is linear interpolation. Do you need someting more sophisticated? Then have a look at en.wikipedia.org/wiki/Interpolation

    – Klaus Gütter
    Nov 17 '18 at 8:11






  • 2





    @Boris that's what you get for not providing a good set of examples when asked to.

    – John
    Nov 17 '18 at 8:12











  • just a resize dude, as when you stretch or shrink an image, I really dont get whats so hard to understand, I feel like I'm speaking a totally different language from anyone else, dont know how else to explain it, sorry

    – Boris
    Nov 17 '18 at 8:48






  • 1





    @Boris as a programmer and an amateur photographer, I'll tell you that when it comes to resizing an image Photoshop gives me a lot of resampling options, including: bicubic, nearest neighbour, and bilinear. There are a vast number of interpolation algorithms, etc. With a single example, it's impossible to tell which one you want. Klaus believed that linear was the method you wanted, now you're saying it's not.

    – John
    Nov 17 '18 at 9:08


















2














I suggest this solution for the case that the new array is shorter than the old one:



int array1 = new int { 1, 3, 5, 7, 9 };
int array2 = new int[4];

for (var i = 0; i < array2.Length; i++)
{
var doubleIndex1 = (double)i * array1.Length / array2.Length;
var index1 = (int)Math.Floor(doubleIndex1);
var rel = doubleIndex1 - index1;

array2[i] = (int)Math.Round((1.0 - rel) * array1[index1] + rel * array1[index1 + 1]);
}





share|improve this answer
























  • yeah, it works that way but only when the values in the array are linearly growing or decreasing, thanks anyway

    – Boris
    Nov 17 '18 at 7:47













  • Well yes, this is linear interpolation. Do you need someting more sophisticated? Then have a look at en.wikipedia.org/wiki/Interpolation

    – Klaus Gütter
    Nov 17 '18 at 8:11






  • 2





    @Boris that's what you get for not providing a good set of examples when asked to.

    – John
    Nov 17 '18 at 8:12











  • just a resize dude, as when you stretch or shrink an image, I really dont get whats so hard to understand, I feel like I'm speaking a totally different language from anyone else, dont know how else to explain it, sorry

    – Boris
    Nov 17 '18 at 8:48






  • 1





    @Boris as a programmer and an amateur photographer, I'll tell you that when it comes to resizing an image Photoshop gives me a lot of resampling options, including: bicubic, nearest neighbour, and bilinear. There are a vast number of interpolation algorithms, etc. With a single example, it's impossible to tell which one you want. Klaus believed that linear was the method you wanted, now you're saying it's not.

    – John
    Nov 17 '18 at 9:08
















2












2








2







I suggest this solution for the case that the new array is shorter than the old one:



int array1 = new int { 1, 3, 5, 7, 9 };
int array2 = new int[4];

for (var i = 0; i < array2.Length; i++)
{
var doubleIndex1 = (double)i * array1.Length / array2.Length;
var index1 = (int)Math.Floor(doubleIndex1);
var rel = doubleIndex1 - index1;

array2[i] = (int)Math.Round((1.0 - rel) * array1[index1] + rel * array1[index1 + 1]);
}





share|improve this answer













I suggest this solution for the case that the new array is shorter than the old one:



int array1 = new int { 1, 3, 5, 7, 9 };
int array2 = new int[4];

for (var i = 0; i < array2.Length; i++)
{
var doubleIndex1 = (double)i * array1.Length / array2.Length;
var index1 = (int)Math.Floor(doubleIndex1);
var rel = doubleIndex1 - index1;

array2[i] = (int)Math.Round((1.0 - rel) * array1[index1] + rel * array1[index1 + 1]);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 17 '18 at 7:41









Klaus GütterKlaus Gütter

2,60231422




2,60231422













  • yeah, it works that way but only when the values in the array are linearly growing or decreasing, thanks anyway

    – Boris
    Nov 17 '18 at 7:47













  • Well yes, this is linear interpolation. Do you need someting more sophisticated? Then have a look at en.wikipedia.org/wiki/Interpolation

    – Klaus Gütter
    Nov 17 '18 at 8:11






  • 2





    @Boris that's what you get for not providing a good set of examples when asked to.

    – John
    Nov 17 '18 at 8:12











  • just a resize dude, as when you stretch or shrink an image, I really dont get whats so hard to understand, I feel like I'm speaking a totally different language from anyone else, dont know how else to explain it, sorry

    – Boris
    Nov 17 '18 at 8:48






  • 1





    @Boris as a programmer and an amateur photographer, I'll tell you that when it comes to resizing an image Photoshop gives me a lot of resampling options, including: bicubic, nearest neighbour, and bilinear. There are a vast number of interpolation algorithms, etc. With a single example, it's impossible to tell which one you want. Klaus believed that linear was the method you wanted, now you're saying it's not.

    – John
    Nov 17 '18 at 9:08





















  • yeah, it works that way but only when the values in the array are linearly growing or decreasing, thanks anyway

    – Boris
    Nov 17 '18 at 7:47













  • Well yes, this is linear interpolation. Do you need someting more sophisticated? Then have a look at en.wikipedia.org/wiki/Interpolation

    – Klaus Gütter
    Nov 17 '18 at 8:11






  • 2





    @Boris that's what you get for not providing a good set of examples when asked to.

    – John
    Nov 17 '18 at 8:12











  • just a resize dude, as when you stretch or shrink an image, I really dont get whats so hard to understand, I feel like I'm speaking a totally different language from anyone else, dont know how else to explain it, sorry

    – Boris
    Nov 17 '18 at 8:48






  • 1





    @Boris as a programmer and an amateur photographer, I'll tell you that when it comes to resizing an image Photoshop gives me a lot of resampling options, including: bicubic, nearest neighbour, and bilinear. There are a vast number of interpolation algorithms, etc. With a single example, it's impossible to tell which one you want. Klaus believed that linear was the method you wanted, now you're saying it's not.

    – John
    Nov 17 '18 at 9:08



















yeah, it works that way but only when the values in the array are linearly growing or decreasing, thanks anyway

– Boris
Nov 17 '18 at 7:47







yeah, it works that way but only when the values in the array are linearly growing or decreasing, thanks anyway

– Boris
Nov 17 '18 at 7:47















Well yes, this is linear interpolation. Do you need someting more sophisticated? Then have a look at en.wikipedia.org/wiki/Interpolation

– Klaus Gütter
Nov 17 '18 at 8:11





Well yes, this is linear interpolation. Do you need someting more sophisticated? Then have a look at en.wikipedia.org/wiki/Interpolation

– Klaus Gütter
Nov 17 '18 at 8:11




2




2





@Boris that's what you get for not providing a good set of examples when asked to.

– John
Nov 17 '18 at 8:12





@Boris that's what you get for not providing a good set of examples when asked to.

– John
Nov 17 '18 at 8:12













just a resize dude, as when you stretch or shrink an image, I really dont get whats so hard to understand, I feel like I'm speaking a totally different language from anyone else, dont know how else to explain it, sorry

– Boris
Nov 17 '18 at 8:48





just a resize dude, as when you stretch or shrink an image, I really dont get whats so hard to understand, I feel like I'm speaking a totally different language from anyone else, dont know how else to explain it, sorry

– Boris
Nov 17 '18 at 8:48




1




1





@Boris as a programmer and an amateur photographer, I'll tell you that when it comes to resizing an image Photoshop gives me a lot of resampling options, including: bicubic, nearest neighbour, and bilinear. There are a vast number of interpolation algorithms, etc. With a single example, it's impossible to tell which one you want. Klaus believed that linear was the method you wanted, now you're saying it's not.

– John
Nov 17 '18 at 9:08







@Boris as a programmer and an amateur photographer, I'll tell you that when it comes to resizing an image Photoshop gives me a lot of resampling options, including: bicubic, nearest neighbour, and bilinear. There are a vast number of interpolation algorithms, etc. With a single example, it's impossible to tell which one you want. Klaus believed that linear was the method you wanted, now you're saying it's not.

– John
Nov 17 '18 at 9:08






















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349037%2fstretch-or-resample-1d-array%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

List item for chat from Array inside array React Native

Jo Brand

Thiostrepton