Save each output step in rows in a larger matrix

2 views (last 30 days)
Avik Mahata
Avik Mahata on 7 Jan 2022
Edited: Jon on 7 Jan 2022
From the below code I get k number of Q1. I am trying to save each of the Q1 in Row1, Row2, Row3..... Rowk. I have been doing the same for the i and j loop but for some reason its failing when I apply the same thing for the Q1. I appreciate any suggestion.
clear all
S = dir('F.*');
for k = 1:numel(S)
T = dir('Na-XYZ.*');
for k = 1:numel(T)
Nadump = dlmread(T(k).name, ' ', 9, 0);
Fdump = dlmread(S(k).name, ' ', 9, 0);
L1 = length(Nadump);
L2 = length(Fdump);
for i=1:L1
for j=1:L2
X(i)= sqrt((Fdump(j,3)-Nadump(i,3))^2 + (Fdump(j,4)-Nadump(i,4))^2 + (Fdump(j,5)-Nadump(i,5))^2);
X(i) = X(i)/10;
Y(j,i) = sort(X(i));
Y1 = Y';
Y2 = sort(Y1);
S1= sort ((Y2(1,:))');
ind1 = S1(:,1) < .28;
ind2 = S1(:,1) < .55;
ind3 = S1(:,1) < .79;
A1 = S1(ind1,:);
A2 = S1(ind2,:);
A3 = S1(ind3,:);
Q1 = [length(A1), (length(A2)-length(A1)), length(A3)-length(A2), 125-length(A3)];

Accepted Answer

Jon on 7 Jan 2022
Edited: Jon on 7 Jan 2022
You have a number of problems with your code.
The main problem, if you want to save the results of each iteration in variable Q, then the left hand side of Q needs to have an ndex specified. Otherwise you are just writing over the value of Q with each iterartion. So you need something like
Q(k,:) = length(A1), (length(A2)-length(A1)), length(A3)-length(A2), 125-length(A3)];
where k is the index of your for loop.
Now another problem that you have is that you have two nested for loops, both using the same index variable, k.
This doesn't make any sense. If you have two nested loops then they each need their own index.
So for example
for k = 1:numel(S)
T = dir('Na-XYZ.*');
for m = 1:numel(T)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!