Fill an array according to the values of a different array

10 ビュー (過去 30 日間)
Patrick Benz
Patrick Benz 2021 年 3 月 12 日
コメント済み: Patrick Benz 2021 年 3 月 17 日
I have 2 arrays. one is A = 21x2 array and the other is B = 700x2 array.
Exemplary:
A= 24 33
26 34
27 33
In the end, B should be structured in such a way that the value from the 1st column is entered in B as often as it is in the second column (33 lines with "24") of A.
The second problem i've got is that I want to fill an array with the output of a function but hence the function is using
Tiefe=normrnd(mu_Tiefe, sigma_Tiefe);
I want to create new random numbers for every cell of the array.
Is it possible to do that without a loop or do I have to use a loop for that?
Or is it mayby easier to create arrays containing the random numbers and pass the arrays to the function?

採用された回答

Aghamarsh Varanasi
Aghamarsh Varanasi 2021 年 3 月 15 日
編集済み: Aghamarsh Varanasi 2021 年 3 月 15 日
Hi Patrick,
Here is a small script for filling array based on another array.
% Lets say
%A = 24 3
% 22 5
% 34 2
A = [24 3; 22 5; 34 2];
B = zeros(1,sum(a(:,2)));
count = 1;
for i = 1:length(A)
idx = zeros(1,length(B));
idx(count : count + A(i,2)) = 1;
B(logical(idx)) = A(i,1);
count = count + A(i,2);
end
For generating an array of random numbers refer to this documentation page.
  1 件のコメント
Patrick Benz
Patrick Benz 2021 年 3 月 17 日
Thx for the small script.
Is there maybe a different approach to do it without a loop?
For the second case, I think I didn't really point out what I was looking for. But I solved the the issue.
When using normrnd
normrnd(mu_Tiefe, sigma_Tiefe,Anzahl,1)
the third and fourth parameter are for the size of the array.

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

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!