MATLAB: Change from 1 to -1 change from 1 to -1 Hi to all , I have a Matrix A = ones(5). How i can change the element value one by one to -1. example A = [1,1,1] A = [-1,1,1] A = [-1,-1,1] A = [-1,-1,-1] But I want to do it for 5 X 5 matrix Regards Best Answer Do you mean iteratively in a loop? E.g., changing elements one at a time in column order:n = numel(A);for k=1:n A(k) = -1; % Use A hereend Related SolutionsMATLAB: -1 and 1 matrix Method One:>> N = 5;>> 2*mod(ndgrid(1:N,1:N),2)-1ans = 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1Method Two:>> 2*(ones(N,1)*mod(1:N,2)).'-1ans = 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1Method Three:>> repmat(2*mod(1:N,2).'-1,1,N)ans = 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 -1 1 1 1 1 1 MATLAB: Flipping the numbers 1 to -1 Try using a while loop:a = ones(1,4)k=1;mag=[];while k<=numel(a) x = rand()>=0.5; a(k)=a(k)-2*x; mag(end+1)=sum(a); k=k+x;endProduces:a = -1 -1 -1 -1mag = 4 4 2 2 2 0 -2 -4 Related QuestionCall certain rows to form a listPlot +1 +1 -1 -1 rectangular pulseChange 1 to -1 in a matrixRANDOM FLIPPING FROM 1 TO -1
Best Answer