Can someone help with my syntax error
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm having trouble getting this to iterate through all rows of my matrix.
the goal is simply to be able to input a random matrix, and use 2 for loops to have the program locate and fprint the row and column index for all occurences of the integer. I can't use the find function, yes this is homework.
댓글 수: 4
Walter Roberson
2021년 4월 4일
As a guideline... beyond approximately five short lines, people are not going to be willing to type in code to test with. And even with short lines, if the question has to do with syntax errors, it is best to post the exact code as there could be hidden characters or something subtle that is important.
채택된 답변
Image Analyst
2021년 4월 4일
We can't run or fix an image. But this will be useful to you instead of x and y
[rows, columns] = size(a)
for col = 1 : columns
for row = 1 : rows
if a(row, col) == 5
fprintf('a = %f at row=%d and column = %d.\n', a(row, col), row, col);
end
end
end
Or vectorized, using the find() function, which you're not allowed to use for this homework problem:
matchingMap = a == 5;
[matchingRows, matchingColumns] = find(matchingMap)
댓글 수: 0
추가 답변 (3개)
KSSV
2021년 4월 4일
If A is your array to count number of times a number n (say 5) appears, follow:
tol = 10^-5 ;
idx = abs(A-n)<=tol ;
nnz(idx(:))
Walter Roberson
2021년 4월 4일
for b = 1 : numel(a)
Caution: length() is not the same as width() . If the array has more rows than columns, then the length() would be the number of rows, same as height()
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!