Filter löschen
Filter löschen

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 Ansicht (letzte 30 Tage)
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 Kommentar
Stephen23
Stephen23 am 17 Jul. 2017
Bearbeitet: Stephen23 am 17 Jul. 2017
"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:

Melden Sie sich an, um zu kommentieren.

Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 17 Jul. 2017
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 Kommentare
Steven Lord
Steven Lord am 17 Jul. 2017
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 am 17 Jul. 2017
i just want to show index of array, like a11(row 1, col 1), a12(row2, col2) .... with my answers.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by