i need to read matrix from a png file

i'm new to matlab and i need help regarding my assignment
to start with i was given 3 png files
  • image1.png,
  • image2.png,
  • image3.png
each image is consist of a 30x30 matrix each element in the matrix has a value from 0-50
1. i have to use the imread function to 'read' the matrix
  • A = imread(image1.png);
  • B = imread(image2.png);
  • C = imread(image3.png);
question; is there a way that i can read all the 3 matrix without writing the imread program three times as shown above?? using a loop perhaps???
2.next is i have to set a limit to each of the element any number from 0-50
example: limit = input(enter a limit: ')
question: if the limit entered is greater than 50, how do you tell matlab to ask the user to input another value until it is within the range acceptable?
3. Say i;ve set the limit to 30
question how do i write a program that will count the number of element in the each of matrix which exceeds the value of the limit?
the result should look like this;
  • A has 135 elements which exceeded the limit
  • B has......................................
  • C has......................................
thanks

답변 (3개)

Matt Tearle
Matt Tearle 2011년 5월 5일

1 개 추천

answer (1) Yes, a for loop will do the job. You need a string in each pass through the loop to represent the file name. You can achieve this a couple of ways. One is to build the string by concatenating the components. You will need num2str. The other is to get a listing of the filenames, then index into that list. You will need to use ls or dir.
answer (2) A while loop will do this for you.
answer (3) Take a look at the nnz function. You will like it.
Paulo Silva
Paulo Silva 2011년 5월 5일

0 개 추천

for ni=1:3
I{ni}= imread(sprintf('image%s.png',num2str(ni)));
end
limit=-1;
while (limit<0) | (limit>50)
disp('')
limit=input('enter the limit [0-50]:')
disp('')
end
fprintf('A has %d elements which exceeded the limit\n',sum(sum(sum(I{1}>limit))))

댓글 수: 3

Matt Tearle
Matt Tearle 2011년 5월 5일
Paulo, maybe Alexis is using the word "assignment" to mean something fairly general, but I think this may be a homework problem...
Paulo Silva
Paulo Silva 2011년 5월 5일
Hi Matt, that's homework for sure and usually I don't do homeworks but sometimes I'm looking for a challenge, the code I post isn't that good on purpose so the OP can improve it and understand how it works, with your detailed explanation I'm sure the OP will do it.
Sean de Wolski
Sean de Wolski 2011년 5월 5일
I hear you on that Paulo. It means we're due for a Hump Day Challenge.

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

avenge.
avenge. 2011년 5월 13일

0 개 추천

Hey guys,
just to let you know, this "homework" looks a lot like a 25% first year Engineering Computing assignment I was given just a few days ago.
What do you think are the odds that this question is actually the same one, given that the timing is almost perfect??? ;)

댓글 수: 1

Matt Tearle
Matt Tearle 2011년 5월 13일
Well, all the components are pretty standard tasks that are asked on probably hundreds of coursework assignments all the time. But if the combination and all the details are the same, it seems likely to be the same assignment. I've seen very similar questions here before -- could be the same person with multiple usernames, could be different people from the same class, could be random coincidence.

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

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

질문:

2011년 5월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by