How can I store values from nested for loop in an array?(storing all the values)

I am trying to store the some values in 2 arrays but not able to find what the problem is. array_poyndb = zeros(1,8); array_avg = zeros(1,10); for j = 1:10 for k = 1:8 filename_E = sprintf('VW471_433_Rxprobe_PW_%d_%d_90_%sefe.txt',j, (k-1)*45, pos_name{j}); filename_H = sprintf('VW471_433_Rxprobe_PW_%d_%d_90_%shfe.txt', j, (k-1)*45, pos_name{j}); elec = load(filename_E); magnetic = load(filename_H); E = [elec(:,4)+elec(:,5)*1i elec(:,6)+elec(:,7)*1i elec(:,8)+elec(:,9)*1i]; H = [magnetic(:,4)+magnetic(:,5)*1i magnetic(:,6)+magnetic(:,7)*1i magnetic(:,8)+magnetic(:,9)*1i]; S = cross(E,conj(H)); poyn = abs(S); poynangle = angle(S); modu = sqrt(sum(poyn.^2,2)); poyndb = 10*log10(modu) - 3; array_poyndb(:,k) = poyndb; disp(k) disp(array_poyndb) end
array_avg(1,10) = mean(array_poyndb);
disp(array_avg)
plot(Pos, array_avg);
end
this is my code I want to store the values in 2 arrays that is array_poyndb(8 rows, 10 columns) and array_avg(1 row, 10 columns). Could anybody help me?

1 Comment

Please use the "{} Code" button to format your code. When it is not readable, answering is impeded.
You have mentioned, that you do not know where the problem is. But could you explain what the problem is? If is much easier to suggest an improvement than to guess, what a problem is.

Sign in to comment.

Answers (1)

I guess, you do not want:
array_avg(1,10) = mean(array_poyndb);
but
array_avg(1, j) = mean(array_poyndb);
Creating the mean over the complete array_poyndb array might be a mistake. Perhaps it is better to calculate the mean outside the loops.
Perhaps
array_poyndb(:,k) = poyndb;
should be replaced by
array_poyndb(j,k) = poyndb;
but I'm not sure about this.

2 Comments

I can now store the values in the array_poyndb(8x10) and as you said that I should calculate the mean outside the loop, but I am getting an error saying Subscripted assignment dimension mismatch.
I tried using the following statement, sum_poyndb = sum(array_poyndb); array_avg(1,j) = sum_poyndb(j)/8; I got the correct array of 1x10 Could you please tell me that is the below statement correct Pos = {'Pos1' 'Pos2' 'Pos3' 'Pos4' 'Pos5' 'Pos6' 'Pos7' 'Pos8' 'Pos9' 'Pos10'}; plot(Pos(j), array_avg);

Sign in to comment.

Categories

Tags

Asked:

on 2 Aug 2017

Commented:

on 2 Aug 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!