How to create a nested loop through a matrix to search a value starting from first column to the last.

1 view (last 30 days)
Hi our Community on MATLAB,
I have specific problem to solve for a matrix of 143X143 which have values ​​only 0 and 1. Please see the screenshot.
Below is the discription of tasks need to be performed.
1 - Starting fro first column, seach the place (row number) of value 1. For example it is at row number 3 for first column.
2 - If the value 1 is found in the column at 'n' th row, then jump to the 'n' th column and search for value 1 in that. For Example, value 1 has been found at row 3 so jump to column 3 and search value 1. And so on.
3 - The loop must break when there are no 1 found in the column (all zero column). And then the same process must start from column 2 this time untill all zero column.
4 - The output I need is the count of the jumps for each columns untill all zeros column.
I have attached the excel file of the data as DAG.xlsx.
I request the MATLAB community to please help me to develop the code for this task.
Please feel free to write if my explaination is not understandble.
Thank you very much in advance.
Best regards

Answers (1)

Rhea Chandy
Rhea Chandy on 1 Apr 2021
Edited: Rhea Chandy on 1 Apr 2021
If you can provide the code you've written to solve the problem and ask a specific question about where you're facing difficulty, we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial ( to quickly learn the essentials of MATLAB.
To get started, you can take a look at this previously answered question, to get an idea of how to go through the elements of the matrix using if, else loops


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by