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

1 view (last 30 days)
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 on 22 Sep 2020
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];

Tore Henriksen Eliassen on 22 Sep 2020
Okay! thanks dude that was very Helpful and does exactly what i need it to.
Would this be apliccable for any unknown A?
Gonna use it in a score calculating function :)
the cyclist on 22 Sep 2020
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 on 22 Sep 2020
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.

Ameer Hamza on 22 Sep 2020
Edited: Ameer Hamza on 22 Sep 2020
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);

Show 1 older comment
Ameer Hamza on 22 Sep 2020
Which part do you want to automize? I wrote the value of throwmatrix = [1 3; 3 1; 4 1; 5 1]; because question didn't defined throw.
Tore Henriksen Eliassen on 22 Sep 2020
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];
A = [1,1,3,4,5,1];
Sorry for the confusion
Tore Henriksen Eliassen on 22 Sep 2020
Edited my post

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by