using if else statements to assign a value to a variable based on the value of a specific element in a matrix

8 次查看(过去 30 天)
I've created a matrix and I'm trying to write a for loop and within the for loop, and I have a variable, p, that I want to change based on a specific element within a matrix that changes with every loop
Here's what I have:
if M(j,k)>0
p(j,k)=0.9;
elseif M(j,k)<0
p(j,k)=0.1;
elseif M(j,k)==0
p(j,k)=0.5;
end
However, when I try to run it, it just creates p as equalling 0.5 and I get an "index in position 2 exceeds array bounds"
Here's an example of how I'm trying to run it
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1)
So what I'm trying to say here is that if we have value j, modified by y, and value k, modified by z1, looking at that placement in matrix M, if its above 0, then p = 0.9

采纳的回答

Hassaan
Hassaan 2024-4-19
编辑:Hassaan 2024-4-19
An initial idea:
% Example matrix M
M = randn(10,10); % A 10x10 matrix with random values
% Initialize matrix p with the same size as M
p = zeros(size(M));
% Assuming y and z1 are vectors that map to indices in M
y = 1:length(M); % Example index mapping, adjust as per your requirements
z1 = 1:length(M); % Example index mapping, adjust as per your requirements
% Loop over the indices
for j = 1:length(y)
for k = 1:length(z1)
if M(y(j), z1(k)) > 0
p(y(j), z1(k)) = 0.9;
elseif M(y(j), z1(k)) < 0
p(y(j), z1(k)) = 0.1;
else
p(y(j), z1(k)) = 0.5;
end
end
end
disp(p)
0.9000 0.1000 0.9000 0.1000 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.9000 0.1000 0.9000 0.9000 0.9000 0.9000 0.9000 0.9000 0.1000 0.9000 0.1000 0.9000 0.1000 0.1000 0.1000 0.9000 0.9000 0.1000 0.9000 0.9000 0.1000 0.1000 0.1000 0.9000 0.9000 0.9000 0.1000 0.9000 0.1000 0.1000 0.9000 0.9000 0.1000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.9000 0.9000 0.1000 0.1000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.9000 0.1000 0.1000 0.9000 0.9000 0.9000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.9000 0.1000 0.1000 0.1000 0.9000 0.9000 0.9000 0.9000
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  3 个评论
Hassaan
Hassaan 2024-4-19
Yes. Adjust as per your needs.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by