Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

Replicate from duplicate in MATLAB

Replicate each element of a row vector (with NaN) a constant number of times.

Examples n=2, A=[1 2 3] -> [1 1 2 2 3 3]

n=0, A=[2 1] -> []

Prerequisite:




Code:

x=input('Enter the array:');
n=input('Enter the number:');
Y=[];
for i=1:length(x)
    m=x(i);
    for i=1:n
        Y=[Y m];
    end
end

Explanation:



Output:





JAVA CODE:

import java.util.Scanner;
class Ghin
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the array length:");
int l=obj.nextInt();
int a[]=new int[l];
for(int i=0;i<l;i++)
{
System.out.println("Enter the number:");
a[i]=obj.nextInt();
}
System.out.println("Enter the number you want to replicate:");
int r=obj.nextInt();
int m=0;
int op=0;
int u[]=new int[l*r];
for(int i=0;i<l;i++)
{
m=a[i];
for(int p=0;p<r;p++)
{
u[op]=m;
op=op+1;
}
}
System.out.println("The replicated matrix is:");
for(int i=0;i<l*r;i++)
{
System.out.print(u[i]+" ");
}
}
}

OUTPUT:

Example 1:

If we want to replicate 0 times , no output is shown :



Example 2:



No comments

Popular Posts

Followers