Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Matlab from treating a 1xn matrix as a column vector

Tags:

arrays

matlab

I'm very frustrated with MATLAB right now. Let me illustrate the problem. I'm going to use informal notation here.

I have a column cell vector of strings called B. For now, let's say B = {'A';'B';'C';'D'}.

I want to have a matrix G, which is m-by-n, and I want to replace the numbers in G with the respective elements of B... For example, let's say G is [4 3; 2 1]

Let's say I have a variable n which says how many rows of G I want to take out.

When I do B(G(1:2,:)), I get what I want ['D' 'C'; 'B' 'A']

However, if I do B(G(1:1,:)) I get ['D';'C'] when what I really want to get is ['D' 'C']

I am using 1:n, and I want it to have the same behavior for n = 1 as it does for n = 2 and n = 3. Basically, G actually is a n-by-1500 matrix, and I want to take the top n rows and use it as indexes into B.

I could use an if statement that transposes the result if n = 1 but that seems so unnecessary. Is there really no way to make it so that it stops treating my 1-by-n matrix as if it was a column vector?

like image 533
user3450598 Avatar asked Nov 19 '25 03:11

user3450598


1 Answers

According to this post by Loren Shure:

Indexing with one array C = A(B) produces output the size of B unless both A and B are vectors.

When both A and B are vectors, the number of elements in C is the number of elements in B and with orientation of A.

You are in second case, hence the behaviour you see.

like image 150
Luis Mendo Avatar answered Nov 20 '25 16:11

Luis Mendo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!