How to store the non zero value from each column of a matrix as a column vector (but if there is only zeros on the column store zero) in each iteration?

1 回表示 (過去 30 日間)
How to store the non zero value from each column of an output matrix which its size(5*3) as a clounm vector with a size(3*1) (but if there is only zeros on the column store zero) in each iteration? Then combine all the 3*1 vector in one matrix call it Outputs.
For example : This output from an iteration of the following program
y =
0.38 0 0
0 0 0
0 0.58 0.71
0 0 0
0 0 0
in each iteration there are different loctions for the non zerovalue and in each column there is just only one non zerovalue.
Can anyone help me in this problem?
This is the program:
mf1=[fismf("trimf" , [0 0 2], 'Name', "ZE") ;
fismf("trimf" , [2 6 10], 'Name', "PS") ;
fismf("trimf" , [10 14 18], 'Name', "M") ;
fismf("trimf" , [18 22 26], 'Name', "PL") ;
fismf("trapmf" , [26 27 60 60], 'Name', "PVL") ]
for i = 1:127
y = evalmf(mf1,RNF(:,i));
end
  5 件のコメント
M
M 2022 年 3 月 6 日
@Arif Hoq and if there is only zeros in the cloumn, I want to store the zero
M
M 2022 年 3 月 6 日
Is that possible to achieve?

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2022 年 3 月 6 日
編集済み: Stephen23 2022 年 3 月 6 日
The simplest solution is probably to use MAX:
M = [0.38,0,0;0,0,0;0,0.58,0.71;0,0,0;0,0,0]
M = 5×3
0.3800 0 0 0 0 0 0 0.5800 0.7100 0 0 0 0 0 0
V = max(M,[],1).'
V = 3×1
0.3800 0.5800 0.7100
An example where one column contains only zeros:
M = [0.38,0,0;0,0,0;0,0,0.71;0,0,0;0,0,0]
M = 5×3
0.3800 0 0 0 0 0 0 0 0.7100 0 0 0 0 0 0
V = max(M,[],1).'
V = 3×1
0.3800 0 0.7100
If the values can be negative:
M = [0.38,0,0;0,0,0;0,-0.58,0.71;0,0,0;0,0,0]
M = 5×3
0.3800 0 0 0 0 0 0 -0.5800 0.7100 0 0 0 0 0 0
V = (max(M,[],1)+min(M,[],1)).'
V = 3×1
0.3800 -0.5800 0.7100
" I want to combine all the column vectors in matrix at the end of the program"
Store them in a cell array, then concatenate them together after the loop:
N = number of iterations
C = cell(1,N)
for k = 1:N
V = the output of your calculation
C{k} = V;
end
M = horzcat(C{:}) % or VERTCAT
  2 件のコメント
Stephen23
Stephen23 2022 年 3 月 6 日
"but how can I combine your solution in for loop and incroporate it with my code? and at the end of the iterations store all the column vectors in one matrix?"
Have a look at the end of my answer. I showed you how.

サインインしてコメントする。

その他の回答 (1 件)

Arif Hoq
Arif Hoq 2022 年 3 月 6 日
編集済み: Arif Hoq 2022 年 3 月 6 日
if there is only 1 zero in a column
A=[0.38 0 0
0 0 5
0 0.58 0.71
0 0 5
0 0 5];
idx=A==0;
for n=1:size(A,2)
if sum(idx(:,n))>1 % if there is more than 1 zero
Output=A(A>0);
elseif sum(idx(:,n))==1 % if there is only 1 zero
Output=[A(A>0);0]
end
end
Or if more than 1 zero in the column
A=[0.38 0 0
0 0 0
0 0.58 0.71
0 0 0
0 0 0];
idx=A==0;
for n=1:size(A,2)
if sum(idx(:,n))>1 % if there is more than 1 zero
Output=A(A>0)
else sum(idx(:,n))==1 % if there is only 1 zero
Output=[A(A>0);0]
end
end
  3 件のコメント
M
M 2022 年 3 月 6 日
Also, I want to combine all the column vectors in matrix at the end of the program
M
M 2022 年 3 月 6 日
As well as your program doesnt store zero if the there is only zero the column of the matix.
I mean if the output from an iteration as the following:
y =
0.38 0 0
0 0 0
0 0 0.71
0 0 0
0 0 0
The output should be as the following:
output =
0.3800
0
0.7100

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by