How to operate on different rows of the same matrix?

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
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.
First of all I need to do the pairing job as I showed in the example.
Then I must work with elements 2nd, 3rd and 4th of each row, following the same pattern of combination. I am sure that if I do properly the first task, the second one will come by itself because it's the same mechanics
You can generate the row indices like this:
nchoosek(1:size(A,1),2)
Marco Boesso
Marco Boesso am 28 Okt. 2020
Bearbeitet: Marco Boesso am 28 Okt. 2020
That looks brilliant! Thanks, will give it a go and feedback you later.
Your solution is brilliant and is helping me, anyway, I'm struggling now with another issue.
The actual matrix is
1 4 -2 0 r0
2 0 -2 0 r1
3 0 -3 0 r0
4 2 -2 0 r1
which is resumed by the matrix A which you've seen in the question, containing the numbers, while there is a cell vector B in this way:
B = {"r0"; "r1"; "r0"; "r1"}
The indexes of rows, thanks for your suggestions, are:
1 2
1 3
1 4
2 3
2 4
3 4
so I went to compare the strings to see if they're equal or not.
That's:
stessa_stanza = strcmp(B(indici(1:end,1)), B(indici(1:end,2)))
Pardon for the names, I'm Italian. "stessa_stanza" is a boolean which aimes to see whether the strings match or not. "indici" are the indexes of rows aforementioned.
When I do this, it comes out all zeros. Which is definetely incorrect, because you can see that some strings match so I should get some 1 in the results. I already tried to change "(" to "{" but it then says that I'm putting too many informations. I don't understand why Matlab isn't cooperating now... :(
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.
Went for some trials, I figured out how to handle those strings by my own. Looks like I've got to compare each string manually.
Nevertheless, thanks for your help: it's been vital to allow me to go on with my work. Brilliant mate :D
Rik
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
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

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
I need a generalised solution as I will put this part in a software which can deal with matrices whose dimensions can vary from time to time
@rik: yes, you got the point. Anyway, it looks like your suggestion is valid, I'm testing it in order to see whether it is good or not so keeping fingers crossed and will update you later! :D
@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
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.

Gefragt:

am 28 Okt. 2020

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by