16 views (last 30 days)

Hi,

I have 4 matrices: X1=[1;2;3;4] X2=[ 5,6,7,8] X3=[9,10,11,12] X4=[13,14,15,16]

I would like to compare these matrices to each other and i want to create a new matrix depending on this comparison.

So I want to create a Y=[yij] 4x4 matrix which consists of matrices.

Now i'll explain the comparing rule with an example.

Here min(X1)= 1 and the max(X1)=4

Let's take x ∈ X2.

If x satisfy min(X1) <= x <= max(X1) it should be 1 in a new matrix , if it does not satisfy min(X1)<= x <= max(X1) it should be 0 in a new matrix.

So in this example i need a matrix :[ 0; 0; 0; 0 ]

According to this rule, i want to create a matrix Y=[yij] which consist of these comparison matrices.

So the first row of Y should consist of 4 elements in other words 4 different matrices related to X1. ( comparison of X1 with itself and others )

- First element of this matrix is y11 which consists of comparing X1 with itself. ( y11= [1;1;1;1], since every element satisfies the rule)
- Second element is y12 which consists of comparing X1 with X2. (y12=[0;0;0;0], since none of the elements satisfies the rule)
- Third element is y13 which consists of comparing X1 with X3.
- Last element of the first row is y14 which consists of comparing X1 with X4.

In this way, i want to create a 4x4 matrix Y.

Is there any way to do this with for loop?

Thanks.

Guillaume
on 6 Feb 2020

Edited: Guillaume
on 6 Feb 2020

First thing: a matrix of matrices does not exist mathematically, so what you're asking is an impossibility. In matlab, you can store matrices in a cell array. cell arrays are not matrices and work differently so may not be what you want. For example you can't perform mathematical operations on a cell array.

Second thing: Numbered variables are always a bad idea and will greatly complicate your code. You're embedding an index in the variable name which is problematic. Instead you should be using a cell array or a 2D matrix to store your vectors. Either way, you then use standard matlab indexing to refer to each vector.

I would recommend you store your X vectors as rows of a matrix:

X = [1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16];

X(1, :) is your original X1, etc.

With that storage, what you want can be done easily without a loop. As said it can't be stored as a 2D matrix. I'd recommend a 3D matrix instead

%Better X for demo

X = [1 2 3 4 5; ... x1

3 5 7 9 11; ... x2

2 4 6 8 10; ... x3

3 9 12 15 18]; % x4

minx = min(X, [], 2); %min of each vector

maxx = max(X, [], 2); %max of each vector

result = X >= reshape(minx, 1, 1, []) & X <= reshape(maxx, 1, 1, [])

result is a numvector x numcolumn x numvector matrix, where result(r, :, p) is the comparison of X(r, :) with X(p, :).

Guillaume
on 6 Feb 2020

After fixing several typos in the code, you'll find it works properly. Sorry, I should have tested it first.

You'll see that result(1, :, 1) (X1 compared with X1) is indeed [1 1 1 1 1] and result(1, :, 2) (X1 compared with X2) is indeed [1 1 0 0 0]

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.