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

John D'Errico
John D'Errico 2021년 4월 4일
Please don't [ost a picture of code. That makes it difficult to help you, as we cannot then paste in your code into MATLAB. Is there a good reason why you want to make it more difficult for someone to help you?
Brendan Clark
Brendan Clark 2021년 4월 4일
Sorry, kinda new here if you can't tell. From what I saw on other posts, people just wanted to see an example of what you've already done. So far people have been quite helpful based upon my screenshots, in this as well as other posts that I've made. My goal was to get recomendations on new syntax that might help me complete the problem. I wasn't specifically looking for someone to solve it completely for me.
If this format is something that bothers you, it may be good to recommend the creation of new post guidlines for newcomers so that we know the specific format thats easiest for you. This program is not intuitive to me, and neither are the specific actions that make it easier for people to assist. The act of asking for information in this manor is to ask to be taught. Do not assume people are attempting to make it harder for you, we're just trying to learn a new subject in the best way that we currently know how.
Walter Roberson
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.
Brendan Clark
Brendan Clark 2021년 4월 4일
Thats good information to have, I appreciate the information and I'll make sure my future posts follow these guidelines.

댓글을 달려면 로그인하십시오.

 채택된 답변

Image Analyst
Image Analyst 2021년 4월 4일

1 개 추천

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)

추가 답변 (3개)

KSSV
KSSV 2021년 4월 4일

0 개 추천

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(:))

댓글 수: 1

Brendan Clark
Brendan Clark 2021년 4월 4일
I don't think I phrased that well, the main goal is to use a set of for loops to find the row and column index for each occurence. The code I originally posted works for one row, but not multiple, I just need to get it to iterate through any avaialable rows.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2021년 4월 4일

0 개 추천

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()
Brendan Clark
Brendan Clark 2021년 4월 4일

0 개 추천

I had attempted to use numel and still didn't get the correct outcome, this could have been due to another factor though.
I didn't realize that length was the same as height, thank you

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020b

태그

질문:

2021년 4월 4일

댓글:

2021년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by