필터 지우기
필터 지우기

I am working on MATLAB for first time and I am stuck in my code, I am unable to change the answer values that should represent Matrix indexes. but it is shown like Aij with every position, plz help how Aij will change to A11, A12, A13 and so on.

조회 수: 1 (최근 30일)
num_row=size(A,1);
num_cols=size(A,2);
for i=1: 1:num_rows
for j=1: 1:num_cols
Aij=de2bi(A(i,j),'left-msb') %Error on Aij , I want to change it as well with my matrix, my matrix is 16*16%
end
end
  댓글 수: 1
Stephen23
Stephen23 2017년 7월 17일
편집: Stephen23 2017년 7월 17일
"how Aij will change to A11, A12, A13 and so on"
This is how beginners write buggy slow code. When you use indexing then your code will be neat, efficient, easier to debug, and more reliable. The MATLAB documentation states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"
Read this to know more about why creating or accessing variables names dynamically is a bad way to write code, and also what the better alternatives are:

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

답변 (2개)

Stephen23
Stephen23 2017년 7월 17일
편집: Stephen23 2017년 7월 17일
Define an anonymous function and call it using arrayfun:
fun = @(n) de2bi(n,'left-msb');
out = arrayfun(fun, A, 'UniformOutput',false)
  댓글 수: 3
Guillaume
Guillaume 2017년 7월 17일
@Stephen, anonymous functions and functional programming (arrayfun) may be a little bit too advanced a topic for somebody starting with matlab.
Jan
Jan 2017년 7월 17일
@Rabia Nazli: Please use flags only to mark inappropriate contributions, which needs to be reviews by the admins or editors. Thanks.

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


Steven Lord
Steven Lord 2017년 7월 17일
The output from de2bi is not guaranteed to be scalar, nor is it guaranteed to be the same size for all elements in A. Therefore, storing the results in a regular numeric array is probably not going to work. Storing each element in an individual variable is discouraged (as Stephen mentioned) and doesn't scale to very large A matrices.
Instead, I would store the results in a cell array.
% Sample data
A = randi(100, 5, 5);
% Cell array that is the same size as A, used to store the results
C = cell(size(A));
for whichElement = 1:numel(A)
C{whichElement} = de2bi(A(whichElement), 'left-msb');
end
Note that I used curly braces {} to write into the contents of the specified cell in C. If I had used () to assign into C, that would have overwritten the specified cell not the contents of that cell. If you think of the cell array C as a filing cabinet, y = C{1} accesses the contents of the first drawer while y = C(1) represents pulling the first drawer out of the filing cabinet entirely.
To use the output of de2bi on a particular element of A, access the contents of the corresponding cell in C.
C{3} % matches de2bi(A(3))
  댓글 수: 3
Steven Lord
Steven Lord 2017년 7월 17일
Add a semicolon to the end of the statement inside the nested for statements. Otherwise it will show you all of C each time it updates one of the elements; that's quite a lot of text to display in the Command Window.
The cell array is like a "filing cabinet" so what you're seeing displayed are the drawers (with a summary of what's inside.) To see the stuff stored inside a drawer, index into the cell array with curly braces.
C{3, 1}
Note that we've been assuming you actually need the output of de2bi for each element of A all at once. Perhaps if you describe what you're planning to do with this information we may be able to offer a different approach to do what you want without having to use anonymous functions, cell arrays, or other somewhat advanced techniques.
Rabia Nazli
Rabia Nazli 2017년 7월 17일
i just want to show index of array, like a11(row 1, col 1), a12(row2, col2) .... with my answers.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by