From: nordmoon on
Hi,

I am having some difficulty on how to address a problem in matlab. I am going to try to explain a simplification of the problem here.

I have for example matrix containing:

A =
400 value 1 value 2 value 3
401 value 4 value 5 value 6
402 value 7 value 8 value 9

Let say that

Column 1 represent temperature
Column 2 represent a number 1
Column 2 represent a number 2
Column 2 represent a number 3

Now I have a another vector containing these numbers between 1-3:

B =
1
2
1
1
3

What I would like to do is to extract the information from matrix A such that for T = 401 I obtain the values in the matrix such that they represent the numbers in vector B. Like, for T= 401 we get

C =
value 4
value 5
value 4
value 4
value 6

I hope that make sense?

Is there a way of doing this easily? I have a very large vector 6000 numbers which I have to find this but its taking a very long time.
From: dpb on
nordmoon wrote:
> Hi,
>
> I am having some difficulty on how to address a problem in matlab. I
> am going to try to explain a simplification of the problem here.
>
> I have for example matrix containing:
>
> A = 400 value 1 value 2 value 3 401 value 4 value 5 value 6 402 value
> 7 value 8 value 9
>
> Let say that
>
> Column 1 represent temperature Column 2 represent a number 1 Column 2
> represent a number 2 Column 2 represent a number 3
>
> Now I have a another vector containing these numbers between 1-3:
>
> B = 1 2 1 1 3
>
> What I would like to do is to extract the information from matrix A
> such that for T = 401 I obtain the values in the matrix such that
> they represent the numbers in vector B. Like, for T= 401 we get
>
> C = value 4 value 5 value 4 value 4 value 6
>
> I hope that make sense?
>
> Is there a way of doing this easily? I have a very large vector 6000
> numbers which I have to find this but its taking a very long time.

T=x(x(:,1)=401); % vector of specific temp value
for i=1:length(B), c(i)=T(B(i)); end

since you're concatenating more values from the B vector than the number
of values per temperature, there's not a direct form for indirect
addressing after the selection; hence the loop. BTW, preallocate C to
length(B)

--
From: nordmoon on
I get the message that

??? Index exceeds matrix dimensions.

A = [ 400 2001 2002 20003 2004 ; 401 3001 3002 3003 3004; 402 5001 5002 5003 5004]
B =[ 1 ;2; 1; 1; 3]

T=A(A(:,1)==401); % vector of specific temp value
C = zeros(length(B),1)
for i=1:length(B), c(i,1)=T(B(i,1)); end
??? Index exceeds matrix dimensions.

What could be wrong?
From: dpb on
nordmoon wrote:
> I get the message that
>
> ??? Index exceeds matrix dimensions.
>
> A = [ 400 2001 2002 20003 2004 ; 401 3001 3002 3003 3004; 402 5001 5002 5003 5004]
> B =[ 1 ;2; 1; 1; 3]
>
> T=A(A(:,1)==401); % vector of specific temp value
> C = zeros(length(B),1)
> for i=1:length(B), c(i,1)=T(B(i,1)); end
> ??? Index exceeds matrix dimensions.
>
> What could be wrong?

> T=A(A(:,1)==401,:); % vector of specific temp value

My typo originally, sorry.

I'll leave as "exercise for student" to try both forms from command line
to see what the problem was and understand why...

--

From: nordmoon on
Well A(:,1)==401 produces a vector

ans =

0
1
0

And T=A(A(:,1)==401)

T =

401

But

for i=1:length(B), c(i,1)=T(B(i,1));
end

add values in T for the different numbers in B ??

I am sorry but I don't see what the error is other than it adds value that is not a matrix, T? How do I correct it?
 |  Next  |  Last
Pages: 1 2
Prev: Normalized matrix
Next: Ezplot constants?