How to manipulate array?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I'm just approaching with matlab programming. My problem is how to manipulate arrays that result from a function.
As example consider:
matrix = [1 2 3, 4 5 6, 7 8 9]
[number_rows, number_columns] = size (matrix)
If I wanted to use number_columns in some calculation i have to write the code 2 times:
[number_rows, number_columns] = size (matrix)
my_columns = number_columns
How can I use the second element of the array immediately?
I'm sure it is easy
0 Commenti
Risposte (2)
Adam
il 22 Gen 2020
Modificato: Adam
il 22 Gen 2020
Just use it! Where you are going to use my_columns, just use number_columns, e.g.
newMatrix = zeros( number_rows, number_columns );
Note though that in your example:
matrix = [1 2 3, 4 5 6, 7 8 9]
just produces a matrix with a single row and 9 columns. The , in there are no different to spaces. Using ; would give a 3-by-3 matrix is that is what you expect.
0 Commenti
Steven Lord
il 22 Gen 2020
For the specific example of the size function, if you want to know the number of columns in the input array, tell size that you want the size of the input array in the second dimension.
A = ones(3, 4);
numColumns = size(A, 2)
You don't have to assign it to a variable, you can use it inside another function call like this.
disp("A has " + size(A, 2) + " columns.")
Note that there can be a difference between size with a dimension and size without.
B = ones(3, 4, 5);
[numRows, numColumnsTimesNumPages] = size(B)
numColumns = size(B, 2)
See the description of the sz1, ... szN output arguments on the size documentation page for an explanation.
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!