[Scilab-users] extracting elements of a matrix
Samuel Gougeon
sgougeon at free.fr
Sat Jan 23 22:24:04 CET 2016
Le 23/01/2016 22:17, Samuel Gougeon a écrit :
> Le 23/01/2016 17:43, fujimoto2005 a écrit :
>> Let X be a m×n matrix and f(i) is a column index for the ith row.
>> I want to get a m×1 vector y where y(i)=X(i,f(i)) for 1<=i<=m.
>> Is there any method to get y other than the following code?
>> for i=1:m
>> y(i)=X(i,f(i));
>> end
> Yes:
> y = X((f-1)*m+i)
with i = 1:m. So:
y = X((f-1)*m + 1:m)
Example:
--> m = 7; n = 5;
--> // X = grand(m, n,"uin", 0, 9)
X = [
2. 5. 9. 9. 0.
2. 8. 0. 3. 1.
4. 3. 9. 9. 2.
5. 5. 4. 6. 9.
4. 5. 7. 6. 9.
1. 0. 6. 6. 8.
9. 6. 9. 3. 6.
]
-->i = 1:m
i =
1. 2. 3. 4. 5. 6. 7.
-->// f = grand(1, m,"uin", 1, n)
f = [ 1. 4. 4. 3. 4. 5. 2. ]
-->X((f-1)*7+i)
ans =
2.
3.
9.
4.
6.
8.
6.
SG
More information about the users
mailing list