Determine the frequency of each matrix element in A and store it as follows in vector a .The first element of a contains the frequency of the smallest element in A and the last element of a contains the frequency of the largest element in A. where A is a 100*100 matrix.

댓글 수: 2

Stephen23
Stephen23 2015년 11월 17일
What have you attempted so far?
Do you want to learn how to solve your own code tasks, or do you just want someone to do your homework for you?
Ankit
Ankit 2015년 11월 17일
편집: Ankit 2015년 11월 17일
Thanks
In case you want to know what i have tried so far is
K = zeros(10000);
for i = 1:10000
j = A(i);
K(i) = find(j == A(i));
end for
But I have a doubt on second part of the Question.

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

 채택된 답변

Stephen23
Stephen23 2015년 11월 17일
편집: Stephen23 2015년 11월 17일

0 개 추천

To start I would highly recommend that you learn to read the documentation of every function and operation that you use. When you read the documentation then you will learn how to use MATLAB correctly. Several mistakes show that you did not read the documentation. Sadly beginners seem to be allergic to reading the documentation, even though it explains how to use functions correctly and can be searched easily using any internet search engine.
You also did not check that each line does what you want it to do. For every line of code that you write you should check that it does what you want it to. This means confirming the size and values from every line that you write: one simple way is to look at the output variables in your Workspace Browser and perform your own sanity check.
For a start you write K = zeros(10000), which creates a matrix of size 10000x10000, with 100 million elements in in. Why do you need one hundred million elements? Perhaps you really meant to write zeros(1,10000), which produces a vector with just 10000 elements. Even better would be to write zeros(1,numel(A)), because then your code will adjust automatically to the size of A.
Then you perform this
j = A(i);
k(i) = find(j == A(i));
but if you define j as being A(i), then this is equivalent to
k(i) = find(A(i) == A(i));
which will always be true (for non-NaN values) because you are simply checking if the scalar value A(i) is equal to itself. And because you are comparing scalars, there is only one element and so the output of find will always be 1. So k is just one big matrix full of ones. Did you look at this? This is why checking your code is important: if you looked at the variable k and saw that every single value was 1, then you would know that something was not doing what you expected it to.
Also if you had looked at k, you would have noticed that you have two similarly named variables: k and K. You preallocated a matrix of zeros to the variable K, but then inside the loop you accessed a different variable k, and K is never used. This is easily detected by checking that every line does what you need it to do. Also the MATLAB editor helps you by letting you highlight all instances of a variable (just click on it) and giving warnings about growing variables inside loops.
You also write end for, which is incorrect syntax. The correct syntax is clearly described in the documentation (which you should check for each function and operation that you use). Pay attention to the warnings that the MATLAB editor gives you, as these are very useful for writing good code!
Your task was to "Determine the frequency of each matrix element...", but your code does not count the elements in any way. Without counting in some way you cannot get a frequency. Even if the loop used find to locate the index of each element, this still has nothing (directly) to do with frequency.
Advice
Forget about loops and read about unique and histc (or histcounts for newer MATLAB versions). I just solved the task in one short line, using only these commands.
These tutorials are an excellent way to start to learn MATLAB:
and read this too:

댓글 수: 2

Ankit
Ankit 2015년 11월 17일
Thanks for your Guidance.
But at beginner level i would like to use the loop not any inbuild methods.
Thanks a lot.
Stephen23
Stephen23 2015년 11월 17일
편집: Stephen23 2015년 11월 17일
Why do you need to use loops? Usually they just make code slow and are a waste of space. MATLAB lets you create very fast and tidy vectorized code. As a beginner, which would you rather learn?
PS: you might want to read this:

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

추가 답변 (0개)

카테고리

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

질문:

2015년 11월 17일

편집:

2015년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by