MATLAB: Loop counting problem in matrix I m new in matlab programming can anyone please give me the code that count a number from a matrix where column 1 have value 1 && column 4 have value 1. thank you Best Answer YourMatrix(:, SomeColumn) == SomeValue & YourMatrix(:, SomeOtherColumn) == SomeOtherValuewould give you a vector of 1 and 0 indicating whether the condition held for that row.With a vector of 1 and 0, how could you count the number of 1's ? Related SolutionsMATLAB: How to select specific columns in a matrix and create a new one Eithernewmatrix = yourmatrix(:, yourmatrix(end, :) == 1)Ornewmatrix = yourmatrix(:, logical(yourmatrix(end, :)))will do it.This is very basic indexing so you should go through the Getting Started Tutorial. What the above does is:get a logical row vector of which columns are 1 and which are 0, using either yourmatrix(end, :) == 1 or logical(yourmatrix(end, :)),then use that logical vector of 0 and 1 to filter the column with yourmatrix(:, logicalvector) MATLAB: How to count the number of times a number appear in a vector Please try the following: A = [ 1 2 3 1 1 6 3 4 2 2 1 1 1 ]; x = unique(A); N = numel(x); count = zeros(N,1); for k = 1:N count(k) = sum(A==x(k)); end disp([ x(:) count ]);HTH.Rick Related QuestionHow to add the corresponding values in different matricesFor loop using unique valuesMatrix Question and row operations with a conditionUsing horzcat in a loop for combining large number of files
Best Answer