How to change a matrix within a function
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Amir Belgi
am 22 Nov. 2017
Beantwortet: Walter Roberson
am 22 Nov. 2017
I want to pass a matrix into a function and to change the matrix within the function. For example, in my case I want to set some elements of the matrix:
function setRedComponent(Image,val)
Image(:,:,1)=val;
end
Image is a 3D matrix representing an RGB image.
I know I can return the image, but I prefer to use the function as follows:
I=zeros(64,64,3);
setRedComponent(I,255);
and not
I=zeros(64,64,3);
I = setRedComponent(I,255);
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 22 Nov. 2017
function setRedComponent(Image,val)
Image(:,:,1)=val;
inname = inputname(1);
if ~isempty(inname)
assignin('caller', inname, Image);
end
end
This style of coding is not recommended at all!
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!