How can i create a for loop that modifies a column in an existing matrix?

1 次查看(过去 30 天)
So i want to make a for loop that takes the startmatrix(given under):
dicevalue = [1,2,3,4,5,6]';
zeroes = [0,0,0,0,0,0]';
startmatrix = [dicevalue,zeroes];
Startmatrix =
1 0
2 0
3 0
4 0
5 0
6 0
Then replaces the zeroes with the values from amount(given under) according to the values from throwvalues(given under)
throw = [1,1,3,4,5,1];
throwvalues = unique(throw)';
amount = histc(throw(:),throwvalues);
throwmatrix = [throwvalues,amount];
Throwmatrix =
1 3
3 1
4 1
5 1
My overall hope is that i somehow can make a matrix that combines the values from 1 to 6 with the values from my amount variable to in the end get something like this:
Finishedmatrix =
1 3
2 0
3 1
4 1
5 1
6 0
Thankful for all help i can get :)

采纳的回答

the cyclist
the cyclist 2020-9-22
I would use the histcounts command to do this task:
A = [1,1,3,4,5,1];
dicevalue = [1,2,3,4,5,6]';
throwCounts = histcounts(A,[dicevalue; Inf])';
output = [dicevalue, throwCounts];
  3 个评论
the cyclist
the cyclist 2020-9-22
I am wary of the use of the phrase, "any unknown A". I mean, it will not work if someone enters the cell array {'x','y','zebra'}.
But it should work for a row vector of any length that contain values 1:6.
This is your code, so you're the one who needs to reach a level of understanding of the histcounts function that makes you confident it does what you need. Beware of blindly using code from the internet that you've not made an effort to understand.
Tore Henriksen Eliassen
Okay thank you for the answer and help.
I have tested the code and function a couple of times by using my dicethrow simulating function and for the purpose i wanted it to serve it works like a charm.
And yeah im aware of the fact that i need to understand the code before using it, wich is why i tried to work myself around the problem by myself before asking for help. I also tested your code to make sure it ended up looking the way i imagnied my problem would end up looking.
So thanks alot for the help and insight.

请先登录,再进行评论。

更多回答(1 个)

Ameer Hamza
Ameer Hamza 2020-9-22
编辑:Ameer Hamza 2020-9-22
If startmatrix and throwmatrix follow the same pattern as you gave in the question, i.e., the first columns of both matrices are always in increasing order, then you can do something like this.
A = [1,1,3,4,5,1];
dicevalue = [1,2,3,4,5,6]';
zeroes = [0,0,0,0,0,0]';
startmatrix = [dicevalue,zeroes];
throwmatrix = [1 3; 3 1; 4 1; 5 1];
idx = ismember(startmatrix(:,1), throwmatrix(:,1));
startmatrix(idx, 2) = throwmatrix(:, 2);
Result
>> startmatrix
startmatrix =
1 3
2 0
3 1
4 1
5 1
6 0
Alternative solution:
A = [1,1,3,4,5,1];
dicevalue = [1,2,3,4,5,6]';
zeroes = [0,0,0,0,0,0]';
startmatrix = [dicevalue,zeroes];
throwmatrix = [1 3; 3 1; 4 1; 5 1];
startmatrix(throwmatrix(:,1), 2) = throwmatrix(:,2);
  4 个评论
Tore Henriksen Eliassen
i kinda want the making of the Finished matrix to be automized
I just realized now that i fked up in writing the question :/
Meant to define throw as A so that it would look like this:
throw = [1,1,3,4,5,1];
instead of this:
A = [1,1,3,4,5,1];
Sorry for the confusion

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by