The size of my final matrix is wrong in a nested loop

I am matching the first element in my matrix to each element in the matrix excluding itself, row to second column element to last. The final size of the resulting matrix is [1 2] instead of [7 2]. How do I fix this? I want a final size of [7 2]. Any help will be much appreciated
A = [1 2 3 4 5 6 7 8];
for c = 1:1:size(A,1)
row1 = A(c, 1);
for k = 2:1:size(A, 2)
columns = A(c, k);
Final = [row1 columns];
disp(size(Final))
end
end

 Accepted Answer

Is this what you want to do?
A = [1 2 3 4 5 6 7 8];
for c = 1:1:size(A,1)
row1 = A(c, 1);
Final = [];
for k = 2:1:size(A, 2)
columns = A(c, k);
Final = [Final; row1 columns];
disp(size(Final));
end
end
1 2 2 2 3 2 4 2 5 2 6 2 7 2
disp(Final);
1 2 1 3 1 4 1 5 1 6 1 7 1 8

12 Comments

Yes, this is it.
How is Final affecting the final size of the matrix? And how do I get it to only output [7 2]?
I copied and pasted your modified version of my code in matlab and this is the output for Final
1 2
1 2
1 2
1 3
1 2
1 3
1 4
1 2
1 3
1 4
1 5
1 2
1 3
1 4
1 5
1 6
1 2
1 3
1 4
1 5
1 6
1 7
1 2
1 3
1 4
1 5
1 6
1 7
1 8
Changing this line:
Final = [row1 columns];
to this:
Final = [Final; row1 columns];
causes Final to be accumulated as the k loop iterates, where each row of Final is like you had it before:
[row1 columns]
So the old way was replacing Final with the 1-by-2 vector [row1 columns] each time through the loop, and the new way instead takes that same vector and adds it to the end of Final as a new row.
And of course the new way requires Final to be initialized as an empty array before the k loop:
Final = [];
Check the size only at the end:
A = [1 2 3 4 5 6 7 8];
for c = 1:1:size(A,1)
row1 = A(c, 1);
Final = [];
for k = 2:1:size(A, 2)
columns = A(c, k);
Final = [Final; row1 columns];
end
end
disp(size(Final));
7 2
disp(Final); % remove this if you don't want to see the value of Final
1 2 1 3 1 4 1 5 1 6 1 7 1 8
The first two parts of my follow-up question have been [by you] answered, thank you for that.
The only problem now is that Final in my command window does not look like yours above, it's repititive and larger than 7x2. Look at my Final matrix in my first comment
That's the value of Final on each iteration, so that's actually several matrices, all of them Final, as Final gets new rows added to it (because disp(Final) is inside the k loop).
Specifically: at first, Final is
1 2
then another row is added in the next iteration, so Final becomes
1 2
1 3
and so on until it has 7 rows.
(Notice where the gaps/spaces are in the output - that's how you can tell it's actually several different outputs.)
To change what's output to the Command Window, check where the disp commands are in your code vs mine in my previous comment.
I understand.
Our disps are on the same line, line 14, because I copied your code as is. You should try it in your matlab to see what I mean
A = [1 2 3 4 5 6 7 8];
for c = 1:1:size(A,1)
row1 = A(c, 1);
Final = [];
for k = 2:1:size(A, 2)
columns = A(c, k);
Final = [Final; row1 columns];
end
end
% no disp before here
disp(size(Final));
7 2
disp(Final); % remove this if you don't want to see the value of Final
1 2 1 3 1 4 1 5 1 6 1 7 1 8
Yes, that works. Thanks!
Can you please tell me why this code is failing
Omatrix = [4 2 3;6 8 5;9 0 4];
for b = 1:1:size(Omatrix, 1)
rows = Omatrix (b, 1); %these are the rows
Final = [];
for c = 2:1:size(Omatrix, 2)
columns = Omatrix(b, c);
Final = [Final; rows columns];
end
end
disp(Final) %output is 9 0
% 9 4
%output should be 4 2
%4 3
%......
%....
%9 4
Move the line
Final = [];
to the top, outside of both loops.
In the previous case, there was only one row, so it didn't matter (and I didn't know what the result should be for a multiple-row matrix).

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 2 Apr 2022

Commented:

on 3 Apr 2022

Community Treasure Hunt

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

Start Hunting!