While loop for the elements of an array

Giorgos · Mar 13, 2023 · 1.9K views
Question
I have an array:   a=[1 1 1 1 1 1 1 10 1 1 1 1 1 1 12 1 1 1 1 3]; I want to make a while loop that does the following enas=0; while a(i)==1 % enas=enas+1; end But I don't know how to express it in matlab. Can you help me please?
Expert Answer
Profile picture of Neeta Dsouza
Neeta Dsouza PhD Expert
Answered Aug 25, 2026

Here's how you'd do it:

 

a=[1 1 1 1 1 1 1 10 1 1 1 1 1 1 12 1 1 1 1 3];
enas=0;
k = 1;
while a(k)==1 % 
  enas=enas+1
  k = k + 1
end

But here's how a real MATLAB programmer would do it:

enas = find(a~=1, 1, 'first')-1

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!