i have S= 6*3 matrix and i want to give sequencial name to each element like s(1,1)=p1 ....s(1,2)=p2...
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pratik Anandpara
am 9 Dez. 2016
Beantwortet: Steven Lord
am 9 Dez. 2016
i have random 6*3 matrix and want to give name to each element in sequence like i call p1 than show me the data of s(1,1)
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 9 Dez. 2016
You just need to iterate over the elements of an array? That's easy with linear indexing and does not require creating many scalar variables.
M = magic(5)
for whichElement = 1:numel(M)
fprintf('Element %d of M is %d.\n', whichElement, M(whichElement));
end
Note that this walks down the columns of M first since MATLAB is column-major. But if you need to walk across the rows first, transpose M. Walking down the columns of the transpose of M is like walking across the rows of M itself.
M = magic(5)
Mt = M.';
% Note that I displayed M but I'm indexing into Mt in the code below.
for whichElement = 1:numel(Mt)
fprintf('Element %d of M (row-wise) is %d.\n', whichElement, Mt(whichElement));
end
Also note that the for loop does not need to change at all if I want to display (or operate on) a different sized or shaped M matrix. The only change I would need to make would be to the definition of M itself.
0 Kommentare
Weitere Antworten (1)
KSSV
am 9 Dez. 2016
Why you want do it? You can call them by s(1,:),s(2,:) etc...it is not suggested what you want to do.
8 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Identification finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!