Storing matrices value based on another matrix

I have a 5 by 5 matrix such as A =
0 1 0 0 0
1 0 0 0 1
2 0 0 0 0
0 0 0 0 0
0 0 1 0 0
And another column matrix B =
0.05
0.10
0.15
0.20
0.25
I have to solve the following problems
“If there is a non zero number in A matrix, it will look into same row of B matrix and store the B matrix number in another matrix named as C. If the non-zero number in A matrix is greater than 1, then it will store that number of times of B matrix same row value in C ”.
So, here C will be,
0.10 0.05 0.25 Nan 0.10
0.15 Nan Nan Nan Nan
0.15 Nan Nan Nan Nan
How can I write a code for C? Thanks in advance.

2 件のコメント

dpb
dpb 2019 年 8 月 3 日
What have you done so far and where, specifically, did you have a particular ML question? We don't just solve homework problems w/o seeing effort being made.
The partial solution for C doesn't appear at all to match the output requested from the problem description, however.
Rupayan Saha
Rupayan Saha 2019 年 8 月 3 日
It's not a homework problem. I am working with large number data and in a part of the solution I need to calculate standard deviation from C matrix but at first I need to create C matrix. That is why I wrote the question this way with some simple form of matrix. I am trying to write down the code to create C matrix from A and B. I need help in this point.

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

 採用された回答

the cyclist
the cyclist 2019 年 8 月 3 日
編集済み: the cyclist 2019 年 8 月 3 日

1 投票

Here is a straightforward algorithm:
[M,N] = size(A);
C = nan(max(sum(A,1)),N);
for jj = 1:N
row = 1;
for ii = 1:M
idx = A(ii,jj);
C(row:row+idx-1,jj) = B(ii);
row = row+idx;
end
end

1 件のコメント

Rupayan Saha
Rupayan Saha 2019 年 8 月 3 日
Thank you. It works perfectly.

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

その他の回答 (0 件)

カテゴリ

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by