How to operate on different rows of the same matrix?
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
Good morning folks, I need your help to figure out a possible solution for my issue. I can't find a way out.
Let's consider this matrix A:
1 4 -2 0
2 0 -2 0
3 0 -3 0
4 2 -2 0
I want to operate on the rows, in order to do some operations. Let's stick, for easiness' sake, to this problem: I want to pair the first elements of two different rows. I.e.:
[1, 2]; [1, 3]; [1, 4]; [2, 3]; [2, 4]; [3, 4]
so, it's the first row with the second, the first with the third, the first with the fourth, the second with the third, the second with the fourth, so on...
I had the job done with nested for cycles, but... my supervisor wants me to use a different solution. I need help as I can't find a way out but I desperately need this!
8 Kommentare
Rik
am 28 Okt. 2020
What is it you actually need to do? Whether or not something can be done without a loop very much depends on the specifics.
Marco Boesso
am 28 Okt. 2020
Rik
am 28 Okt. 2020
You can generate the row indices like this:
nchoosek(1:size(A,1),2)
Marco Boesso
am 28 Okt. 2020
Bearbeitet: Marco Boesso
am 28 Okt. 2020
Marco Boesso
am 28 Okt. 2020
Rik
am 28 Okt. 2020
The strcmp function seems to work differently if you put in a cell array or strings, than if you put in a cell array of chars.
In general you should pick one of these two options:
B = {'r0'; 'r1'; 'r0'; 'r1'};
B = ["r0"; "r1"; "r0"; "r1"];
Either will work, I don't know why a cell array of strings doesn't work.
Marco Boesso
am 28 Okt. 2020
Rik
am 28 Okt. 2020
You can fairly easily convert what you have to either style in my comment. I don't see why you would need to do manual work, that doesn't sound like the Matlab way.
Antworten (1)
Sudhakar Shinde
am 28 Okt. 2020
Try this:
[A(1,1) A(2,1)] %[1 2]
[A(1,1) A(3,1)] %[1 3]
[A(1,1) A(4,1)] %[1 4]
[A(2,1) A(3,1)] %[2 3]
[A(2,1) A(4,1)] %[2 4]
[A(3,1) A(4,1)] %[3 4]
5 Kommentare
Rik
am 28 Okt. 2020
Hardcoded values are generally a bad idea if you want something in a pattern. If A is extended with a new row you would have to check which combinations you need to add.
How I understood the question is how to vectorize this:
for ind1=1:(size(A,1)-1)
for ind2=(ind1+1):size(A,1)
A([ind1 ind2],1)
end
end
Marco Boesso
am 28 Okt. 2020
Marco Boesso
am 28 Okt. 2020
Sudhakar Shinde
am 28 Okt. 2020
@rik, Thanks for your suggestion and it will surely helpful for OP. If A is extended, vectorized answer will surely helpful. Need to keep in mind that using a loops will increase computational complexity.
to store the result in cell :
B={};
for ind1=1:(size(A,1)-1)
for ind2=(ind1+1):size(A,1)
B{end+1}=A([ind1 ind2],1);
end
end
Rik
am 28 Okt. 2020
Or you don't use a nested loop with a dynamically exanding variable:
B=nchoosek(1:size(A,1),2);
B=mat2cell(B,ones(size(B,1),1),2);
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!