Clear Filters
Clear Filters

Can't get matrix to populate first column

4 views (last 30 days)
Kitt
Kitt on 20 Apr 2024
Answered: Steven Lord on 20 Apr 2024
So I'm trying to create a matrix that changes between 3 possible values based on modified values of a different matrix, here's the code:
edit: forgot to add cap and some ends
cap=15;
z=linspace(1,cap,cap)';
y=linspace(1,cap,cap)';
f=@(x,y) x-y;
M=f(z.',y);
p=zeros(size(M));
z1=z+1; %dem went to patch 1 and found food
z1(z1>cap)=cap;
for j=1:15
for k=1:15
if M(j,z1(k))>0
p(j,z1(k))=0.9;
elseif M(j,z1(k))<0
p(j,z1(k))=0.1;
else
p(j,z1(k))=0.5;
end
end
end
How do I get that first column to populate correctly?
I have multiple of these p matrices with different modifiers and some of them populate the entire matrix while others are missing a column or row
  5 Comments
Stephen23
Stephen23 on 20 Apr 2024
Edited: Stephen23 on 20 Apr 2024
"How do I get that first column to populate correctly?"
Your code defines the values of z from 1. Then for z1 you add 1 to that, so the lowest z1 value (and column index) will be 2. But your code has no comments or explanation, so we cannot guess which of those operations is correct or incorrect.
Note: a simpler and more efficient approach for using CAP:
z1 = min(z1,cap);
Kitt
Kitt on 20 Apr 2024
z is supposed to represent an information state, and z1 is a modifier of that information state. It's supposed to be a model of learning after watching a demonstrator. So you start with an informational state of 1, and then if you decide to watch a demonstrator your state could change from z to z1, which is a +1 to your info state. p represents the probability of going to a patch, which is based on your info state after watching.
So I guess it doesn't matter that the first column isn't populated then, because if you decide to watch and your state changes to z1 then you'll never be at one. I guess I just didn't fully understand how p was populating.
I'm sorry, I'm really new to matlab, I'm kind of figuring it out as I go. Thank you so much for the help!

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 20 Apr 2024
The smallest value in z is 1. Because you add 1 to z to generate z1, that means the smallest value in z1 is 2. You use z1 to determine which column of M and p to process, so you never process column 1.
But you could avoid the loops using the discretize function or by using the vectorized relational operators.
cap=15;
z=linspace(1,cap,cap)';
y=linspace(1,cap,cap)';
f=@(x,y) x-y;
M=f(z.',y)
M = 15x15
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Define bins [-Inf, 0), [0, eps(0)), and [eps(0), Inf]
edges = [-Inf, 0 , eps(0) , Inf];
values = [ 0.1, 0.5, 0.9];
p = discretize(M, edges, values)
p = 15x15
0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% or
p2 = repmat(0.5, size(M));
p2(M < 0) = 0.1;
p2(M > 0) = 0.9
p2 = 15x15
0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.5000 0.9000 0.9000 0.9000 0.9000 0.9000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
check = isequal(p, p2)
check = logical
1

Community Treasure Hunt

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

Start Hunting!