need to skip iterations in a for loop

3 Ansichten (letzte 30 Tage)
John Hendricks
John Hendricks am 25 Apr. 2022
Kommentiert: John Hendricks am 25 Apr. 2022
I'm trying to set values in a row of a large 2D matrix to 1, but i need to skip every two entries, so have my last row look something like 1001001001 instead of my current L(bottomrow,:)=1. I was thinking about using a for loop but i'm not so sure how to set that up, I tried
L=zeros(1024,1024);
for n=(2:1022:3)
L(1024,n)=1
end
using n from 2 to 1022 because i'm using dummy arrays for my code.

Akzeptierte Antwort

Chunru
Chunru am 25 Apr. 2022
L=zeros(1024,1024);
% for n=(2:1022:3)
% L(1024,n)=1
% end
% Set the last row (first end), column 2 to last column with a step 3 to be
% 1 (you can adjust this for your need)
L(end, 2:3:end) = 1;
L(end, :)
ans = 1×1024
0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0
  1 Kommentar
John Hendricks
John Hendricks am 25 Apr. 2022
Thanks for helping a matlab newbie such as myself :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by