Arduino Serial 1 Second Delay

Hello, I am trying to send 2 vectors from MATLAB and store them into arrays on the arduino. Afterwords I perform the inner product of the vectors and use Serial print to get the answer back into MATLAB. I was able to do this, but I can't do it unless I include "pause(1)" in MATLAB. I'd like to get rid of pause(1) from my code, but if I don't use it then my code doesn't work. What in the Arduino is stopping me from getting rid of it?

Arduino code:

//This program takes in 2 vectors from matlab and does the inner product of 2 vectors
//and prints the results to the Serial port to be read by matlab again.
//The program reads in the Serial data from MATLAB and puts it into arrays, and then the calculations are done.
  int flag = 0; //checks if a 0 has been entered at the end of a string
  int flag2 = 1; // Checks if we are done getting data for the arrays.
  int flag3 = 0; // Checks to clear memory space
  int length; //keeps track of the place within the array
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  float array1[100];
  float array2[100];

  float ibyte = 0;
  
  if(flag3 == 0){//This sets all of the data in the memory locations within the arrays to 0.
      erasememory(array1,array2);}

  if(Serial.available()>0 && (flag2 == 0))
  {
//      Serial.flush();
      ibyte=Serial.parseFloat();

      
      
      if ((ibyte == 0) && (flag == 1)){
        // Serial.println("No more numbers");
        flag2 = 1;}

      if ((ibyte == 0) && (flag == 0)){
//        Serial.println("Changing to Array2");
        flag = 1;
        length = 0;}
        
      if(flag == 0 && (ibyte != 0)){
        array1[length] = ibyte;
        length = length + 1;}
        
      if(flag == 1 && (ibyte != 0)){
        array2[length] = ibyte;
        length = length + 1;}
        

       // delay(800);
      Serial.println(ibyte); //I'd like to remove this, so that way the only serial data being sent back
      //to MATLAB is the answer found later
  }
 if(flag2 == 1) //This essentially gets the 
  {//Arraycheck(array1,array2); This is old and has been replaced, ignore it ,or don't. Im not a cop.
  SetLCD(array1,array2);//This prints out the math done with the vectors
  flag2 = 0;//This resets flags and length so that the arduino can keep running new vectors.
  flag = 0;
  length = 0;}
}

void Arraycheck(float* array1, float* array2)
{s
}
void erasememory(float* array1, float* array2)
{
  int i;
    for(i=0; i<100; i++)
    {
    array1[i] = 0;
    array2[i] = 0;
    }
    flag3 = 1;
}
void SetLCD(float* array1, float* array2)
{
//I am lazy and have no made the LCD part and probably wont.
  Serial.println(math(array1,array2)); 
   
}

float math (float array1[], float array2[])
{
  int i;
  float sum = 0;

  for(i = 0; i < 100; i++)
  {
      sum = array1[i]*array2[i] +sum;
  }
  return sum;
 
}

Matlab code:

%Before running the program make a variable "s" that is 
%ttyS101 may be set in the terminal by
%"sudo ln -s /dev/ttyACM0 /dev/ttyS101"
%"s = serial('/dev/ttyS101', 'BaudRate', 9600);" (for linux folks otherwise
%use COM# for windows)
%fopen(s);
% However, check which port the arduino is connected to it might be ACM1
% or a different port.

fopen(s);
%array1 = [1.05, 1.23, 1.66, -1.43, 2, 0];
%array2 = [-2.21, -.8, 2.08, 1.20, 3, 0];
array1 = [1, 2, 0];
array2 = [6, 7, 0];

length1 = numel(array1);
length2 = numel(array2);

%if the arrays aren't the same size or the arrays dont end
%in 0 then they return an error
if (length1) ~= (length2)
    disp('Arrays are not the same size')
    return
end

if (array1(length1) ~= 0)
    disp('Array1 doesnt end in 0')
    return
end

if (array2(length2) ~= 0)
    disp('Array2 doesnt end in 0')
    return
end

TRASH = fscanf(s, '%c')

disp('Program has started')
for i = 1:length1
    fprintf(s,'%.2f',array1(i));
    pause(1);
    fscanf(s, '%c')
end


for i = 1:length2
    fprintf(s,'%.2f',array2(i));
    pause(1);
    fscanf(s, '%c')
end


y = fscanf(s,'%c')
CorrectAnswer = array1*transpose(array2)
fclose(s);

What is this?

void Arraycheck(float* array1, float* array2)
{s
}

Well, b/c we don't know what happens when you don't have "pause(1)" in there (all we know is that "it doesn't work" whatever that means) it is hard to say what the problem is.

  if(Serial.available()>0 && (flag2 == 0))
  {
//      Serial.flush();
      ibyte=Serial.parseFloat();

Careful here, you are only checking is there is at least 1 byte in the buffer, last I remember a float was 4 bytes. EDIT: It will wait 1000ms for the data to arrive, so this will actually work, given that the incoming data is in valid float format...
Oh and Serial.flush() only waits for serial being sent to finish, it doesn't empty the buffer.

You may find the examples in Serial Input Basics useful. There is also a parse example.

...R

Careful here, you are only checking is there is at least 1 byte in the buffer, last I remember a float was 4 bytes.

Like parseInt(), parseFloat() is a blocking function. It will wait (for a while) for data to arrive,

OP: Pay attention, too, to that statement. How does parseFloat() know that there is no more data for a value? It knows that when some character arrives that is not a valid value for a float (something besides a minus sign, a digit, or a dot) arrives. You never send anything like that, so the function only knows when a float value ends because no data has arrived for some period of time. That is why you have to wait some period of time between sending values. Send some non-float character, like a space, between values, and you won't have to wait uselessly.

Thank you PaulS, I also edited my statement.