Home > Net >  How to create 2D matrix from a variable that its value is changed in every iteration
How to create 2D matrix from a variable that its value is changed in every iteration

Time:11-02

This question has two parts, Before the part I mentioned in the title, first I need to loop through every pixel in the image(the image has 610x340x103) basically, each i, and j coordinate gives me 103 values (depth). I'd like to know if the code I put down does the job. If so it brings me to the second part: I have a function that gives me output "a" let's say. And I put it inside the for loop, so in every iteration, I get a new value for the variable "a". Now I want to do is to create a 2D matrix with 610x340 dimensions from this "a" variable. It is like I am creating my own image only from this variable.

for i=1:1:610
    for j=1:1:340
        allbands=paviaU(i,j,:);
        writematrix(allbands,'allbands.txt','WriteMode','append');

        fftmy=abs(fft(allbands));
        fftmynew=fftmy(1:52);
        [xData, yData] = prepareCurveData( xaxis2, fftmynew );

        % Set up fittype and options.
        ft = fittype( 'exp1' );
        opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
        opts.Display = 'Off';
        [fitresult, gof] = fit( xData, yData, ft, opts );
        values=coeffvalues(fitresult);
        a=values(1);
    end
end

So, how can I modify this code in a way that the value of "a" sits in the exact same position as i and j in every iteration for a new 2D matrix?

CodePudding user response:

You just need to intialise a before the loop and assign to the same indices

a = NaN(610,340);

for i=1:1:610
    for j=1:1:340
        % stuff to work out 'values'...

        a(i,j) =values(1);
    end
end
  • Related