Adding elements in array in Matlab [closed] - arrays

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have to add elements in array.
I have function:
function d = kronDel(j,k)
if j == k
d = 1;
else
d = 0;
end
And i have n=0:31.
I tried this:
x2=j*kronDel(n-2,0);
Why doesnt this work?
I only get x2=0;

You want to make a kronecker delta function but you don't need it.
x2 = j(n==2)
or, if you want to keep the zeros
x2 = j.*(n==2)
If you really want to make the function, just adapt it to:
function d = kronDel(j,k)
d = j==k;
From your comment: "x2=[0,0,j,0,0,0,0...to the 31 all 0s] j=sqrt(-1)"
x2=zeros(1,31);x2(3)=i

Related

How to get 4 characters of a string at a time iterating backwards [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
If I have a string, say "abcdefghij"
How can I iterate and get to characters backwards at a time.
For example
first loop : ghij
second loop: fedc
third loop: ab
Im using C++ but dont feel like its simple enough i should be able to adapt any language
Heres what I have so far:
for(long unsigned int i=0; i<s.length();i+=4){
digits.push_back(std::stoi(s.substr(i, 4)));
}
My issue is that this is left justified, not right justified
In pseudo code:
iterate i from s.length stepping by -4, while i > 0
start = max(i - 4, 0)
part = s.substring(start, i)

How does the code that prints array elements works? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.

When to declarate a variable issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Can someone explain me the most simple way the meaning of this syntax of C?
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(number[i]<number[j])
{
aux=number[i];
number[i]=number[j];
number[j]=aux;
}
}
}
I just trying to figure out I know is an iteration or a loop but specifically aux is a var. Why i need to follow this i'm trying to get pos and negs, into an array but this part i'm stuck is there another way ?
I just need to figure this syntax.
This looks like Bubble Sort. aux is a temporary variable used for exchanging the values of number[i] and number[j]. You can't do
number[i] = number[j];
number[j] = number[i];
to exchange the two, as both would be equal to number[j] this way. So you need a temporary variable.

I need a variable that have a small range value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an input number that it's value is not always consistent, i mean the value change plus 1 or minus 1 and etc. So, i want to compare it with a const but have a small range value, for example a const int Dist that have value between 14 to 16. Is that possible to implement it on C programming? Please help me.
You can set constant for lower bound, and constant for upper bound and check if the value falls within the range.
Pseudocode:
int const LOWER_BOUND = 14;
int const UPPER_BOUND = 16;
if (input <= UPPER_BOUND && input >= LOWER_BOUND)
... logic here ...

how to call more than one element of array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am using an array,i want to call 4 element from the array at the same time so i can use them in an equation ,any one knows how.
example
int a[10]={1,2,3,4,5,6,7,8};
I want 1234+15
how?
using C#:
1234 = 1*1000 + 2*100 + 3*10 + 4*1
double result = 0;
int ex = 3; // Math.Pow(10,3) = 1000.00
for(int i = 0; i < 4 0; i++)
{
result += a[i] * Math.Pow(10, ex);
ex--;
}
result += 15;

Resources