Constraints

1 次查看(过去 30 天)
Melissa
Melissa 2011-10-7
Good Morning All,
I was wondering how it would be possible to apply constraints to a matrix. I will have a large matrix full of answers but I want to limit the answers.
For example let x,y,z be columns 1,2,3 of the matrix and i want to set limitations to 5<x<10, 0<y<4, and 6<z<12.
Any suggestions?
Thanks,
Mel

回答(2 个)

the cyclist
the cyclist 2011-10-7
What do you want to do with the values that lie outside those limits? If you want to cap them, then you could do:
>> x(x>10) = 10;
>> x(x<5) = 5;
and similar for y and z.
If "A" is the matrix and x is the first column, as you say, then this means something like:
>> A(A(:,1)>10,1) = 10;
etc.

Andrei Bobrov
Andrei Bobrov 2011-10-7
xyz = randi([0 28],15,3)
llt = [5 0 6]
rlt = [10 4 12]
id = bsxfun(@lt,llt,xyz)&bsxfun(@gt,rlt,xyz)
out = arrayfun(@(i1)xyz(id(:,i1),i1),1:size(xyz,2),'un',0)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by