find specific value from each row from matrix (Index, and number)

16 次查看(过去 30 天)
Hi All,
I am trying to find/obtain each value is lower than specific number from each row from a matrix. However I only obtain values from all the matrix.
Here is the code below:
I need to obtain only in each row if there is a number lower than 4 or not, I need both the index, and the real value
x=[1 2 3 4 5]
for i=1:5
sigma1(i)=x(i)+1;
sigma2(i)=x(i)+2;
sigma3(i)=x(i)+3;
sigmaa(:,i)=[sigma1(i) sigma2(i) sigma3(i)]
min_va=find(sigmaa>4); % I need to obtain only in each row if there is a number lower than 4 or not
[r,c]=find(sigmaa>4)
end
Any help please!
  1 个评论
Adam Danz
Adam Danz 2019-8-29
That's a lot of lines to do a simple thing. Also, your comments state that you want to identify values less than 4 but your code is identifying values greater than 4.
Consider these two lines
x = [1 2 3 4 5]; %row vector
isLowerThan = (x + [1;2;3]) < 4;
The produce a logica matrix "isLowerThan" where each column corresponds to one value in "x" compared added to [1;2;3]. The '1's (true's) show where the results are less than 4.

请先登录,再进行评论。

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-8-29
编辑:KALYAN ACHARJYA 2019-8-29
I need to obtain only in each row if there is a number lower than 4 or not, I need both the index, and the real value
X=magic(6);
[r c]=find(X<4);
idx=cell(1,length(r));
for i=1:length(r)
idx{i}={r(i),c(i)};
end
idx
Now read the values from X having idx indices (idx cell array, read individually)
  2 个评论
Ali Tawfik
Ali Tawfik 2019-8-31
Thanks for your reply,
That's not what I need, I need for example, the values in the first row, which is lower than 4.
I need the numbers that;s in your example should be
1
3
2
and nothing in the last 3 rows.
thanks,
Adam Danz
Adam Danz 2019-8-31
That wasn't clear in your question. These lines below produce that output.
x = magic(6);
x = x.';
x(x<4)

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by