# why we use flag in matlab code and if we have sequence of integer then how the flag(integer)=1 ?

19 views (last 30 days)
Sultan Mehmood on 25 Jun 2019
Answered: Steven Lord on 25 Jun 2019
intger=(2,6,8,9)
flag(intger)=1;
how it works? how flag select the value of intger

Steven Lord on 25 Jun 2019
This (note the modified first line):
intger=[2,6,8,9]
flag(intger)=1
uses linear indexing to assign to elements 2, 6, 8, and 9 of the flag array. Normally you'd want to preallocate the flag array to some known size before assigning to those specific elements. For instance, if you used the Hilbert matrix example from that blog post, you may want to make flag a logical matrix the same size as the Hilbert matrix.
A = hilb(5)
flag = false(size(A))
intger=[2,6,8,9]
flag(intger)=1
If you did that, you could use that flag matrix to perform logical indexing into A.
elements2_6_8_and_9 = A(flag)
Why is element 9 the element in the fourth row, second column of A rather than the second row, fourth column? MATLAB is column major. The linear order of elements in A is:
orderOfElements = reshape(1:numel(A), size(A))

### Categories

Find more on Matrix Indexing 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!